34
Chapter 4: Control Structures: Selection Visual Basic .NET Programming: From Problem Analysis to Program Design

Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Embed Size (px)

Citation preview

Page 1: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Chapter 4: Control Structures: Selection

Visual Basic .NET Programming:

From Problem Analysis to Program Design

Page 2: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 2

Objectives

• Write and interpret logical expressions

• Write one-way selection statements

• Write two-way selection statements

• Write multi-way selection statements

Page 3: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 3

Writing And Interpreting Logical Expressions

• Make decisions in selection statement by writing logical expressions

• Logical expression

– Specifies condition that evaluates to:

• True

• Or False

– Use to compare two values

Page 4: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 4

Using the VB .NET Relational Operators

• Relational operator

– Use to make comparison in logical expression

Page 5: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 5

Page 6: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 6

Example 4-1: Using the >= relational operator

• examScore – Integer variable

– Contains value 86

• Expression:– examScore >= 90

– Is value contained in exam-score greater than or equal to 90?

– Answer:• False

Page 7: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 7

Using the VB .NET Logical Operators

• Logical operators

– Use to combine logical expressions

• Frequently used logical operators:

– Not

• Negates an expression

– And

• Joins two expressions

Page 8: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 8

Page 9: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 9

Using the VB .NET Logical Operators

• And operator

– Joins two expressions

– Forms compound expression

– If both expressions evaluate to true

• Then compound expression is true

• Otherwise, it is false

Page 10: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 10

Example 4-4: Using the And logical operator

• Variables:

– examScore

• Integer

• Value: 86

– engineeringStudent

• Boolean

• Value: True

Page 11: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 11

Example 4-4: Using the And logical operator

• Expression:

– examScore >= 90 And engineeringStudent

– First expression (examScore >= 90)

• Evaluates to false

– Second (engineeringStudent)

• Evaluates to true

– Result is false

Page 12: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 12

Page 13: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 13

Using the VB .NET Logical Operators

• Or operator

– Joins two expressions

– Returns true if either or both expressions are true

• Xor operator

– Joins two expressions

– Returns true if one and only one expression is true

– Otherwise, returns false

Page 14: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 14

Page 15: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 15

Page 16: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 16

Using the VB .NET Logical Operators

• AndAlso, OrElse

– Correspond to And and Or

– Employ short-circuit evaluation technique

Page 17: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 17

Example 4-8: Using the OrElse logical operator

• Statement:

– 1 < 2 OrElse 2 < 3

• If first expression is true

– Compound expression true

– Eliminates need to evaluate second expression

Page 18: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 18

Writing One-way Selection Statements

• One-way selection statement:

– Evaluates logical expression

– Executes statements only if expression is true

• Two-way selection statement:

– Evaluates logical expression

– Executes statements if it is true

– Executes different statements if it is false

Page 19: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 19

Writing One-way Selection Statements

• Flowchart

– Graphical representation of logic

– Use symbols to represent logical components of algorithm

– Symbols:

• Diamond

• Rectangle

• Circle

• Flow lines

Page 20: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 20

Page 21: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 21

Writing One-way Selection Statements

• Single-line If syntax:

If (logical expression) Then statement

• Multi-line If syntax:

If (logical expression) Then

statement

.

statement

End If

Page 22: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 22

Writing One-way Selection Statements

• Multi-line if:

– Statements written on separate lines

– Keyword End If must be used to terminate If statement

Page 23: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 23

Page 24: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 24

Writing Two-way Selection Statements

• Write two-way selection statement – When you want to execute one or more statements

if logical expression is true

– But also want to execute one or more different statements if it is false

• Nested If– If statement written inside another If statement

– Can replace compound expression with nested If

Page 25: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 25

Page 26: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 26

Writing Two-way Selection Statements

• Syntax:If (logical expression) Then

statement(s)

Else

statement(s)

End If

• ElseIf– Combines Else and If

Page 27: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 27

Example 4-17: Determining a grade using ElseIf statements

1. If examScore >= 90 Then

2. grade = “A”

3. ElseIf examScore >= 80 Then

4. grade = “B”

5. ElseIf examScore >= 70 Then

6. grade = “C”

7. ElseIf examScore >= 60 Then

8. grade = “D”

9. Else grade = “F”

10. End If

Page 28: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 28

Writing Multi-way Selection Statements

• Acts like multi-way If statement

– By transferring control to one or more statements

– Depending on value of a variable

• Sometimes called case structure

• Uses keywords:

– Select

– Case

Page 29: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 29

Example 4-18: Determine a Grade Using Select Case

Statements1. Select Case examScore

2. Case Is >= 90

3. grade = “A”

4. Case 80 To 89

5. grade = “B”

6. Case 70 To 79

7. grade = “C”

Page 30: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 30

Example 4-18: Determine a Grade Using Select Case

Statements…

8. Case 60 To 69

9. grade = “D”

10. Case Else

11. grade = “F”

12. End Select

Page 31: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 31

Programming Example: Payroll Calculation

• Input – Overtime exempt “Y” or “N”

– Hours worked

– Hourly pay rate

• Output– The employee’s

• Regular pay

• Overtime pay

• Total pay

Page 32: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 32

Summary

• Make decisions in selection statement by writing logical expression– Evaluates to either true or false

• Logical operators join two logical expressions to form compound expression

• One-way selection statement – Evaluates logical expression

– Executes one or more statements only if expression is true

Page 33: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 33

Summary (continued)

• Two-way selection statement

– Evaluates logical expression

– Executes one or more statements if it is true

– Executes one or more different statements if it is false

• One-way selection:

– One-line and multi-line If statements

Page 34: Chapter 4: Control Structures: Selection Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 34

Summary (continued)

• Two-way selection:

– If and Else statements

• Multi-way selection structure

– Keywords Select Case