Clearly Visual Basic Programming with Visual Basic 2010

Clearly Visual Basic: Programming with Visual Basic 2010 2 nd Edition Chapter 8 Decisions, Decisions (Selection Structure)

Objectives After studying Chapter 8, you should be able to: Show the selection structure in both pseudocode and a flowchart Write If…Then…Else statements Include comparison operators in a selection structure’s condition Add a check box to an interface 2 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition

Someone Might Need to Make a Decision All computer programs are written using one or more of three control structures Sequence, selection, or repetition Selection structure Makes a decision and then takes appropriate action based on that decision Studying problem specification Determines whether problem’s solution requires a selection structure 3 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition

Figure 8 -1 A problem that requires the sequence structure only 4 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition

Figure 8 -2 A problem that requires the sequence structure and a single-alternative selection structure 5 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition

Figure 8 -3 Another problem that requires the sequence structure only 6 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition

Figure 8 -4 A problem that requires the sequence structure and a dual-alternative selection structure 7 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition

Going Beyond Rob’s Problems Figure 8 -5 shows: Problem specification that does not involve Rob, the mechanical man Correct algorithm for the problem 8 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition

Figure 8 -5 Bonus problem specification and algorithm 9 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition

The If … Then … Else Statement Comparison operators Used to compare values Statement block Set of statements contained in the true path and the false path All expressions containing a comparison operator Will result in an answer of either True or False only 10 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition

Figure 8 -8 If…Then…Else statement and comparison operators (continues) 11 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition

Figure 8 -8 If…Then…Else statement and comparison operators (cont’d. ) 12 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition

Figure 8 -9 btn. Calc control’s Click event procedure 13 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition

Examining Another Problem Specification Flowchart symbols Oval: Start/stop symbol Rectangle: Process symbol Parallelogram: Input/output symbol Diamond: Decision symbol Represents the condition in selection and repetition structures Contains a question whose answer is either True or False 14 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition

Figure 8 -11 Total Due Calculator problem specification and algorithm 15 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition

Figure 8 -15 Completed Click event procedure for the btn. Calc control 16 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition

Hey, That’s Not the Way I Would Have Done It Variable that has procedure scope Can be used anywhere within the procedure Variable that has block scope Can be used only within the statement block in which it is declared Block-level variable Variable declared within a statement block 17 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition

Figure 8 -19 A different way of solving the Total Due Calculator problem 18 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition

Figure 8 -20 Code corresponding to Figure 8 -19’s algorithm 19 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition

Summary Selection structure One of the three control structures Indent instructions in a selection structure’s true path and its false path In Visual Basic: You can use the If…Then…Else statement to code the selection structure An expression containing a comparison operator: Will always evaluate to either True or False Clearly Visual Basic: Programming with Visual 20 Clearly Visual Basic: with Visual Basic 2008 Basic 2010, 2 nd. Programming Edition

Summary (cont’d. ) In a flowchart The selection structure’s condition is represented by a diamond chk The three-character ID used when naming check boxes Block-level variable Can only be used within the statement block in which it is declared 21 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
- Slides: 21