Chapter 4: Control Structures: Selection Visual Basic .NET Programming: From Problem Analysis to Program Design Objectives • Write and interpret logical expressions • Write one-way selection statements • Write two-way selection statements • Write multi-way selection statements Visual Basic .NET Programming: From Problem Analysis to Program Design 2 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 3 Using the VB .NET Relational Operators • Relational operator – Use to make comparison in logical expression Visual Basic .NET Programming: From Problem Analysis to Program Design 4 Visual Basic .NET Programming: From Problem Analysis to Program Design 5 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 6 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 7 Visual Basic .NET Programming: From Problem Analysis to Program Design 8 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 9 Example 4-4: Using the And logical operator • Variables: – examScore • Integer • Value: 86 – engineeringStudent • Boolean • Value: True Visual Basic .NET Programming: From Problem Analysis to Program Design 10 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 11 Visual Basic .NET Programming: From Problem Analysis to Program Design 12 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 13 Visual Basic .NET Programming: From Problem Analysis to Program Design 14 Visual Basic .NET Programming: From Problem Analysis to Program Design 15 Using the VB .NET Logical Operators • AndAlso, OrElse – Correspond to And and Or – Employ short-circuit evaluation technique Visual Basic .NET Programming: From Problem Analysis to Program Design 16 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 17 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 18 Writing One-way Selection Statements • Flowchart – Graphical representation of logic – Use symbols to represent logical components of algorithm – Symbols: • Diamond • Rectangle • Circle • Flow lines Visual Basic .NET Programming: From Problem Analysis to Program Design 19 Visual Basic .NET Programming: From Problem Analysis to Program Design 20 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 21 Writing One-way Selection Statements • Multi-line if: – Statements written on separate lines – Keyword End If must be used to terminate If statement Visual Basic .NET Programming: From Problem Analysis to Program Design 22 Visual Basic .NET Programming: From Problem Analysis to Program Design 23 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 24 Visual Basic .NET Programming: From Problem Analysis to Program Design 25 Writing Two-way Selection Statements • Syntax: If (logical expression) Then statement(s) Else statement(s) End If • ElseIf – Combines Else and If Visual Basic .NET Programming: From Problem Analysis to Program Design 26 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 27 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 28 Example 4-18: Determine a Grade Using Select Case Statements 1. 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” … Visual Basic .NET Programming: From Problem Analysis to Program Design 29 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 30 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 31 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 32 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 Visual Basic .NET Programming: From Problem Analysis to Program Design 33 Summary (continued) • Two-way selection: – If and Else statements • Multi-way selection structure – Keywords Select Case Visual Basic .NET Programming: From Problem Analysis to Program Design 34