600 likes | 690 Views
Chapter 5: More on the Selection Structure. Previewing the Modified Covington Resort Application. Figure 5-2 Recalculated amounts shown in the interface. Figure 5-1 Interface showing the calculated amounts. Lesson A Objectives. After studying Lesson A, you should be able to:
E N D
Previewing the Modified Covington Resort Application Figure 5-2 Recalculated amounts shown in the interface Figure 5-1 Interface showing the calculated amounts Programming with Microsoft Visual Basic 2012
Lesson A Objectives After studying Lesson A, you should be able to: • Include a nested selection structure in pseudocode and in a flowchart • Code a nested selection structure • Desk-check an algorithm • Recognize common logic errors in selection structures • Include a multiple-alternative selection structure in pseudocode and in a flowchart • Code a multiple-alternative selection structure Programming with Microsoft Visual Basic 2012
Nested Selection Structures • Nested selection structure • When either a selection structure’s true path or its false path contains another selection structure • The inner selection is called a nested selection Figure 5-4 A problem that requires a nested selection structure Programming with Microsoft Visual Basic 2012
Nested Selection Structures (cont.) Figure 5-5 A problem that requires two nested selection structures Programming with Microsoft Visual Basic 2012
Flowcharting a Nested Selection Structure Figure 5-6 Problem specification and a correct solution for the voter eligibility problem Programming with Microsoft Visual Basic 2012
Flowcharting a Nested Selection Structure (cont.) Figure 5-7 Another correct solution for the voter eligibility problem Programming with Microsoft Visual Basic 2012
Coding a Nested Selection Structure Figure 5-8 Code for the flowcharts in Figures 5-6 and 5-7 Programming with Microsoft Visual Basic 2012
Logic Errors in Selection Structures • Four common errors: • Using a compound condition rather than a nested selection structure • Reversing the decisions in the outer and nested selection structures • Using an unnecessary nested selection structure • Including an unnecessary comparison in a condition Programming with Microsoft Visual Basic 2012
Logic Errors in Selection Structures (cont.) • Algorithm • The step-by-step instructions for accomplishing a task • Desk-checking • The process of reviewing the algorithm while seated at your desk rather than in front of a computer • Also called hand-tracing • You use a pencil and paper to follow the algorithm’s instructions by hand • Desk-check to make sure you do not miss any instructions, and that existing instructions are correct and in the proper order Programming with Microsoft Visual Basic 2012
Logic Errors in Selection Structures (cont.) Figure 5-11 Sample data and expected results for the algorithm shown in Figure 5-10 Figure 5-10 A correct algorithm for the golf fee procedure Programming with Microsoft Visual Basic 2012
Logic Errors in Selection Structures (cont.) First Logic Error: Using a Compound Condition Rather Than a Nested Selection Structure Figure 5-12 Correct algorithm and an incorrect algorithm containing the first logic error Programming with Microsoft Visual Basic 2012
Logic Errors in Selection Structures (cont.) First Logic Error: Using a Compound Condition Rather Than a Nested Selection Structure (cont.) Figure 5-12 Correct algorithm and an incorrect algorithm containing the first logic error Figure 5-13 Results of desk-checking the incorrect algorithm from Figure 5-12 Programming with Microsoft Visual Basic 2012
Logic Errors in Selection Structures (cont.) Second Logic Error: Reversing the Outer and Nested Decisions Figure 5-14 Correct algorithm and an incorrect algorithm containing the second logic error Figure 5-15 Results of desk-checking the incorrect algorithm from Figure 5-14 Programming with Microsoft Visual Basic 2012
Logic Errors in Selection Structures (cont.) Third Logic Error: Using an Unnecessary Nested Selection Structure Figure 5-16 Correct algorithm and an inefficient algorithm containing the third logic error Figure 5-17 Results of desk-checking the inefficient algorithm from Figure 5-16 Programming with Microsoft Visual Basic 2012
Logic Errors in Selection Structures (cont.) Fourth Logic Error: Including an Unnecessary Comparison in a Condition Figure 5-18 Problem specification, a correct algorithm, and an inefficient algorithm Figure 5-19 Results of desk-checking the algorithms from Figure 5-18 Programming with Microsoft Visual Basic 2012
Multiple-Alternative Selection Structures • When you need to choose from several different options, use a multiple-alternative selection structure • Also called an extended selection structure Figure 5-20 Problem specification for the Allen High School problem Programming with Microsoft Visual Basic 2012
Multiple-Alternative Selection Structures (cont.) Figure 5-21 Pseudocode and flowchart containing a multiple-alternative selection structure Programming with Microsoft Visual Basic 2012
Multiple-Alternative Selection Structures (cont.) Figure 5-23 Excellent message shown in the interface Figure 5-22 Two versions of the code containing a multiple-alternative selection structure Programming with Microsoft Visual Basic 2012
The Select Case Statement • Select Case statement • Used when a multiple-alternative selection structure has many paths from which to choose • It’s simpler and clearer to code the selection structure rather than several If…Then…Else statements Programming with Microsoft Visual Basic 2012
The Select Case Statement (cont.) Figure 5-24 Syntax and an example of the Select Case statement Programming with Microsoft Visual Basic 2012
The Select Case Statement (cont.) Specifying a Range of Values in a Case Clause Figure 5-25 Syntax and an example of specifying a range of values Programming with Microsoft Visual Basic 2012
Lesson A Summary • To create a selection structure that evaluates both a primary and a secondary decision: • Place (nest) the secondary decision’s selection structure within either the true or false path of the primary decision’s selection structure • To verify that an algorithm works correctly: • Desk-check (hand-trace) the algorithm Programming with Microsoft Visual Basic 2012
Lesson A Summary (cont.) • To code a multiple-alternative selection structure: • Use either If…Then…Else statements or the Select Case statement • To specify a range of values in a Select Case statement’s Case clause: • Use the To keyword when you know both the upper and lower values in the range • Use the Iskeyword when you know only one end of the range • The Is keyword is used in combination with one of the following comparison operators: =, <, <=, >, >=, <> Programming with Microsoft Visual Basic 2012
Lesson B Objectives After studying Lesson B, you should be able to: • Include a group of radio buttons in an interface • Designate a default radio button • Include a check box in an interface • Compare Boolean values Programming with Microsoft Visual Basic 2012
Modifying the Covington Resort Application Figure 5-36 Revised TOE chart for the Covington Resort application (continues) Programming with Microsoft Visual Basic 2012
Modifying the Covington Resort Application (cont.) (continued) Figure 5-36 Revised TOE chart for the Covington Resort application Programming with Microsoft Visual Basic 2012
Modifying the Covington Resort Application (cont.) Figure 5-37 Partially completed interface for the Covington Resort application Programming with Microsoft Visual Basic 2012
Modifying the Covington Resort Application (cont.) Adding a Radio Button to the Interface • Radio button • Limits the user to only one choice from a group of two or more related but mutually exclusive choices • The three-character ID for a radio button’s name is rad Figure 5-38 Atrium radio button added to the View group box Programming with Microsoft Visual Basic 2012
Modifying the Covington Resort Application (cont.) Adding a Radio Button to the Interface (cont.) • The automatically selected radio button is called the default radio button • Represents the user’s most likely choice • The first button in the group • Checked property • Set to True to designate the default radio button Programming with Microsoft Visual Basic 2012
Modifying the Covington Resort Application (cont.) Adding a Check Box to the Interface • Unlike radio buttons, check boxes provide one or more independent and nonexclusive items from which the user can choose • Any number of check boxes on a form can be selected at the same time • The three-character ID for a check box’s name is chk Figure 5-39 Vehicle parking fee check box added to the interface Programming with Microsoft Visual Basic 2012
Modifying the Covington Resort Application (cont.) Figure 5-40 Correct TabIndex values Programming with Microsoft Visual Basic 2012
Modifying the Calculate Button’s Code Figure 5-41 Modified pseudocode for the btnCalc_Click procedure Programming with Microsoft Visual Basic 2012
Modifying the Calculate Button’s Code (cont.) Figure 5-42 Modified list of named constants and variables Programming with Microsoft Visual Basic 2012
Modifying the Calculate Button’s Code (cont.) Figure 5-43 Named constants added to the procedure Figure 5-44 Variables added to the procedure Programming with Microsoft Visual Basic 2012
Modifying the Calculate Button’s Code (cont.) • Comparing Boolean Values Figure 5-45 Examples of comparing Boolean values (continues) Programming with Microsoft Visual Basic 2012
Modifying the Calculate Button’s Code (cont.) (continued) Figure 5-45 Examples of comparing Boolean values Programming with Microsoft Visual Basic 2012
Modifying the ClearLabels Procedure • CheckedChanged event • Occurs when the value in a control’s Checked property changes Figure 5-50 ClearLabels procedure Programming with Microsoft Visual Basic 2012
Modifying the ClearLabels Procedure (cont.) Figure 5-51 Covington Resort application’s code at the end of Lesson B (continues) Programming with Microsoft Visual Basic 2012
Modifying the ClearLabels Procedure (cont.) (continued) Figure 5-51 Covington Resort application’s code at the end of Lesson B (continues) Programming with Microsoft Visual Basic 2012
Modifying the ClearLabels Procedure (cont.) (continued) Figure 5-51 Covington Resort application’s code at the end of Lesson B (continues) Programming with Microsoft Visual Basic 2012
Modifying the ClearLabels Procedure (cont.) (continued) Figure 5-51 Covington Resort application’s code at the end of Lesson B (continues) Programming with Microsoft Visual Basic 2012
Modifying the ClearLabels Procedure (cont.) (continued) Figure 5-51 Covington Resort application’s code at the end of Lesson B (continues) Programming with Microsoft Visual Basic 2012
Modifying the ClearLabels Procedure (cont.) (continued) Figure 5-51 Covington Resort application’s code at the end of Lesson B Programming with Microsoft Visual Basic 2012
Lesson B Summary • To limit the user to only one choice in a group of two or more related but mutually exclusive choices: • Use the RadioButton tool to add two or more radio buttons to the form • To include two groups of radio buttons on a form, at least one of the groups must be placed within a container, such as a group box • To allow the user to select any number of choices from a group of one or more independent and nonexclusive choices: • Use the CheckBox tool to add one or more check box controls to the form Programming with Microsoft Visual Basic 2012
Lesson B Summary (cont.) • To determine whether a radio button or check box is selected or unselected: • Use the Checked property of the radio button or check box • The property will contain the Boolean value True if the control is selected; otherwise, it will contain the Boolean value False • To process code when the value in the Checked property of a radio button or check box changes: • Enter the code in the radio button’s or check box’s CheckedChanged event procedure Programming with Microsoft Visual Basic 2012
Lesson C Objectives After studying Lesson C, you should be able to: • Determine the success of the TryParse method • Generate random numbers • Show and hide a control while an application is running Programming with Microsoft Visual Basic 2012
Using the TryParse Method for Data Validation • TryParse method • Converts a string to a number of a specific data type • With successful conversion: • The TryParse method stores the number in the variable specified in the method’s NumericVariableName argument • With unsuccessful conversion: • TryParse stores the number 0 in the variable Programming with Microsoft Visual Basic 2012
Using the TryParse Method for Data Validation (cont.) Figure 5-58 Syntax and an example of using the Boolean value returned by the TryParse method Programming with Microsoft Visual Basic 2012
Using the TryParse Method for Data Validation (cont.) Figure 5-59 Sample run of the original Click event procedure Programming with Microsoft Visual Basic 2012