Clearly Visual Basic: Programming with Visual Basic 2010 2nd Edition Chapter 9 Time to Leave the Nest (Nested Selection Structures) Objectives After studying Chapter 9, you should be able to: Nest selection structures Include logical operators in a selection structure’s condition 2 Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition Nested Selection Structures Nested selection structure Selection structure contained (nested) within an outer structure Figures 9-1 through 9-4 Shown on following slides Show problem specifications and their algorithms Problem specification and algorithm are modified To use single- and dual-alternative selection structures To use nested selection structures 3 Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition Nested Selection Structures (cont’d.) Figure 9-1 A problem that requires a dual-alternative selection structure 4 Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition Nested Selection Structures (cont’d.) Figure 9-2 A problem that requires nested single-alternative selection structures 5 Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition Nested Selection Structures (cont’d.) Figure 9-3 Another problem that requires a dual-alternative selection structure 6 Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition Nested Selection Structures (cont’d.) Figure 9-4 A problem that requires a nested dual-alternative selection structure 7 Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition Putting Rob’s Problems Aside Figure 9-5 Shows problem specification and a correct algorithm Figure 9-6 Shows modified problem specification and its algorithm 8 Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition Putting Rob’s Problems Aside (cont’d.) Figure 9-5 Math problem specification and algorithm 9 Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition Putting Rob’s Problems Aside (cont’d.) Figure 9-6 Modified math problem specification and its algorithm 10 Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition Let’s Go to the Swap Meet Swap the contents of text boxes in Figure 9-9 Swap is accomplished by: Assigning first integer to Number 1 text box Assigning second integer to Number 2 text box Figure 9-10 on next slide Illustrates where to place the swapping instructions 11 Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition Let’s Go to the Swap Meet (cont’d.) Figure 9-10 First modification to the algorithm from Figure 9-6 12 Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition That’s Way Too Logical for Me Logical operators (Boolean operators) Allow you to combine two or more conditions into one compound condition 13 Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition Figure 9-14 Logical operators and examples 14 Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition Figure 9-15 Gross pay problem specification and algorithm 15 Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition Figure 9-16 Code corresponding to Figure 9-15’s algorithm 16 Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition Summary of Operators Figure 9-18 lists operators and their order of precedence Arithmetic operators evaluated first Comparison operators next Then logical operators 17 Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition Figure 9-18 Listing of arithmetic, comparison, and logical operators 18 Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition Summary Both paths in a selection structure: Can include nested selection structures Nested selection structures Used when more than one decision must be made before the appropriate action can be taken When comparing two numbers: The first number can be greater than, less than, or equal to the second number 19 Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition Summary (cont’d.) Logical operators Combine conditions into compound condition When the AndAlso logical operator is used to combine conditions: All conditions must be True for the compound condition to be True AndAlso operator Has a higher precedence than the OrElse operator Arithmetic operators Evaluated first in an expression 20 Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition