1 / 29

Chapter 4: Control Structures: Selection

Chapter 4: Control Structures: Selection. Visual Basic .NET Programming: From Problem Analysis to Program Design. Writing And Interpreting Logical Expressions. Make decisions in selection statement by writing logical expressions Logical expression Specifies condition that evaluates to:

agatha
Download Presentation

Chapter 4: Control Structures: Selection

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4: Control Structures: Selection 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 • An integervariable “examScore” contains the value 86 • Expression: examScore >= 90 • Mening: Is the 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 • Oroperator • Joins two expressions • Returns true if either or bothexpressions are true • Xoroperator • 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. 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 1 (group of) statement if it is true • Executes a different (group of) statements if it is false Visual Basic .NET Programming: From Problem Analysis to Program Design

  15. 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

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

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

  18. 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

  19. Writing One-way Selection Statements • Multi-line if statement: • 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

  20. Writing Two-way Selection Statements • Write a 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

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

  22. 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

  23. 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 (download example) Visual Basic .NET Programming: From Problem Analysis to Program Design

  24. Writing Multi-way Selection Statements • Acts like multi-way Ifstatement • 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. Summary (continued) • Two-way selection: • If and Else statements • Multi-way selection structure • Keywords Select Case For in-class practice: p. 138, Ex. 3, 5 (use And instead of OrElse), 7 (use And instead of AndAlso) p. 139, PEx. 1 (optional), 2, 3 HOMEWORK: p. 139, PEx. 4; Due: 2/19/09 Visual Basic .NET Programming: From Problem Analysis to Program Design

More Related