1 / 32

Chapter 5: More on the Selection Structure

Chapter 5: More on the Selection Structure. Programming with Microsoft Visual Basic .NET, Second Edition. Nested Selection Structures.

loe
Download Presentation

Chapter 5: More on the Selection Structure

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 5: More on the Selection Structure Programming with Microsoft Visual Basic .NET, Second Edition

  2. Nested Selection Structures • When a selection structure’s true path or its false path contains another selection structure, the inner selection structure is referred to as a nested selection structure

  3. Nested If Blocks • Constructs in which an If block is contained inside another If block are referred to as nestedIf blocks. If cond1 Then Action1 Else Ifcond2 Then Action 2 Else Action 3 End If End If Start Yes No Cond 1True? Yes No Cond 2True? Action 1 Action 2 Action 3 Connector(End If) End

  4. Nested Selection Structures (continued)

  5. Nested Selection Structures (continued) Part of Figure 5-3: Pseudocode showing the nested selection structure in the true path

  6. Nested Selection Structures (continued) Part of Figure 5-5: Pseudocode showing the nested selection structure in the false path

  7. Nested If Blocks (Ex #1 ) If Cond1 Then Action 1 Else If Cond2 Then Action 2 Else If Cond3 Then Action3 End If End If End If Draw the flowchart corresponding to this pseudocode

  8. Ex #1 Solution Start If Cond1 Then Action 1 Else If Cond2 Then Action 2 Else If Cond3 Then Action3 End If End If End If Yes No Cond 1True? Yes No Cond 2True? Action 1 No Yes Cond 3True? Action 2 Action 3 End

  9. Nested If Blocks (Ex #2 ) Start Yes No Cond 1True? Yes Yes No No Cond 2True? Cond 3True? No Yes Action 2 Action 1 Cond 4True? Action 3 Action 4 Action 5 Write the pseudocode corresponding to this flowchart End

  10. Ex #2 Solution If Cond1 Then IfCond2Then Action1 Else Action2 End If Else IfCond3Then Action3 Else If Cond4Then Action4 Else Action5 End If End If End If Start Yes No Cond 1True? Yes Yes No No Cond 2True? Cond 3True? No Yes Action 2 Action1 Cond 4True? Action 3 Action 4 Action 5 End

  11. Logic Errors in Selection Structures • Common logic errors with selection structures: • Using a logical operator when a nested selection structure is needed • Reversing the primary and secondary decisions • Using an unnecessary nested selection structure

  12. Logic Errors in Selection Structures (continued) • An algorithm is the set of step-by-step instructions that accomplish a task • Desk-checking means that you use sample data to walk through each of the steps in the algorithm manually, just as if you were the computer • Desk-checking is also called hand-tracing

  13. Logic Errors in Selection Structures (continued) Figure 5-7: A correct algorithm for the vacation procedure

  14. Logic Errors in Selection Structures (continued) Figure 5-8: Results of desk-checking the correct algorithm shown in Figure 5-7

  15. Using a Logical Operator Rather Than a Nested Selection Structure Figure 5-9: Correct algorithm and an incorrect algorithm containing the first logic error

  16. Reversing the Primary and Secondary Decisions Figure 5-11: Correct algorithm and an incorrect algorithm containing the second logic error

  17. Using an Unnecessary Nested Selection Structure Figure 5-13: Correct algorithm and an inefficient algorithm containing the third logic error

  18. The If/ElseIf/Else Form of the Selection Structure • Figure 5-16 shows two versions of the Visual Basic .NET code for the grade procedure Figure 5-15: Letter grades and messages

  19. The If/ElseIf/Else Form of the Selection Structure (continued) Figure 5-16: Two versions of the Visual Basic .NET code for the grade procedure

  20. Select Case Statement • If you are evaluating a variable against a range of conditions (values), the Select Case Statement provides a much easier and more flexible way to handle the decision. Case Optional Condition Case1 Case2 Case3 Case Else Action3 Action4 Action2 Action1

  21. Select Case Statement Select CaseIntAge Case1 (means if age=1) Action 1 Case2,3,4(means if age=2,3or 4) Action 2 Case5 To 10(means if age=510) Action 3 Caseis >11 Action 4 Case Else Action 5 End Select Select CaseTestValue CaseValue1 Action 1 CaseValue2 Action 2 CaseValue3 Action 3 Case Else Action 4 End Select The TestValue and Value expressions must represent the same data type

  22. Select Case Statement (Example) Select Case StrOperator Case“+” SngResult = SngNb1 + SngNb2 Case“-” SngResult = SngNb1 - SngNb2 Case “*” SngResult = SngNb1 * SngNb2 Case Else SngResult = SngNb1 / SngNb2 End Select

  23. The Case Form of the Selection Structure Figure 5-18: Syntax and an example of the Select Case statement

  24. Desk-Checking the Grade Procedure Figure 5-19: Results of desk-checking the grade procedure shown in Figure 5-18

  25. Using To and Is in an ExpressionList • To keyword: specifies a range of minimum and maximum values Case 1 To 5 • Is keyword: specifies a range of values when you know only one value, either the minimum or the maximum Case Is > 10

  26. The Like Comparison Operator • The Like operator allows you to use pattern matching to determine whether one string is equal to another string • Syntax: string Like pattern • Both string and pattern must be String expressions • pattern can contain one or more of the pattern-matching characters • The Like operator evaluates to True if string matches pattern; otherwise it evaluates to False

  27. The Like Comparison Operator (continued) Figure 5-24: Pattern-matching characters

  28. Like (Comparison operator for String Values) • ? Any single character. • * Zero or more characters. • # Any single digit (0-9). • [charlist] Any single character in charlist. • [!charlist] Any single character not in charlist. • If "aBBBa" Like "a*a" Then • If "F" Like "[A-Z]" Then • If "F" Like "[!A-Z]" Then • If "a2a" Like "a#a" Then • If "aM5b" Like "a[L-P]#[!c-e]" Then • If "BAT123khg" Like "B?T*” Then • If "CAT123khg" Like "B?T*” Then Returns True. Returns True. Returns False. Returns True. Returns True. Returns True. Returns False.

  29. Exercise #6 • Write a program that will determine the number of days in a month between the year 2001 and 2017 (if other display warning message) • Inputs: • Year: 2004 or 04 (both should work) • Month: june (or June or JUNE) or 06 (all should work) • As a reminder February has 28 days except in leap years, when it has 29. Recent and upcoming leap years are 2004, 2008, 2012, 2016 • Draw flowchart or pseudo-code before coding (I will collect it )

  30. Summary • To create a selection structure that evaluates both a primary and a secondary decision, place (nest) the selection structure for the secondary decision within either the true path or false path of the selection structure for the primary decision • To code a multiple-path (or extended) selection structure, use either the If…Then…Else statement or the Select Case statement

  31. Summary (continued) • To limit the user to only one choice in a group of two or more related and mutually exclusive choices, add a radio button control • To allow the user to select any number of choices from a group of one or more independent and nonexclusive choices, add a check box control • To call (invoke) a user-defined Sub procedure, use the Call statement

  32. Summary (continued) • To process code when the form is loaded into memory, enter the code in the form’s Load event procedure • To display or hide a control, set the control’s Visible property • To code a check box control’s Click event procedure, use a selection structure to determine whether the check box was either selected or deselected by the user

More Related