620 likes | 789 Views
Programming with Microsoft Visual Basic 2008 Fourth Edition. Chapter Five More on the Selection Structure. Previewing the Math Practice Application. Open Math.exe file The Math Practice application will be used by 1 st and 2 nd grade students .
E N D
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Five More on the Selection Structure
Previewing the Math Practice Application • Open Math.exe file • The Math Practice application will be used by 1st and 2nd grade students Programming with Microsoft Visual Basic 2008, Fourth Edition
Previewing the Math Practice Application (continued) Figure 5-1: Math Practice application’s user interface Programming with Microsoft Visual Basic 2008, Fourth Edition
Lesson A Objectives After studying Lesson A, you should be able to: • Include a nested selection structure in both pseudocode and a flowchart • Code a nested selection structure • Desk-check an algorithm • Recognize common logic errors in selection structures Programming with Microsoft Visual Basic 2008, Fourth Edition
Lesson A Objectives (continued) • Include a multiple-path selection structure in both pseudocode and a flowchart • Code a multiple-path selection structure using the If/ElseIf/Else and Case forms of the selection structure Programming with Microsoft Visual Basic 2008, Fourth Edition
Nested Selection Structures • Selection structure: • Chooses true or false path based on comparison or decision • Nested selection structure: • Selection structure that lies on true or false path of an outer selection structure • Also called an inner selection structure • Two decisions are involved: • Primary decision: Made by outer structure • Secondary decision: Made by inner structure Programming with Microsoft Visual Basic 2008, Fourth Edition
Nested Selection Structures (continued) Figure 5-5: Display Message button’s pseudocode with the nested selection structure in the true path Programming with Microsoft Visual Basic 2008, Fourth Edition
Nested Selection Structures (continued) Figure 5-6: Display Message button’s flowchart with the nested selection structure in the true path Programming with Microsoft Visual Basic 2008, Fourth Edition
Nested Selection Structures (continued) Figure 5-7: Display Message button’s Click event with the nested selection structure in the true path Programming with Microsoft Visual Basic 2008, Fourth Edition
Nested Selection Structures (continued) Figure 5-7: Display Message button’s Click event with the nested selection structure in the true path (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition
Nested Selection Structures (continued) Figure 5-8: Display Message button’s pseudocode with the nested selection structure in the false path Programming with Microsoft Visual Basic 2008, Fourth Edition
Nested Selection Structures (continued) Figure 5-9: Display Message button’s flowchart with the nested selection structure in the true path Programming with Microsoft Visual Basic 2008, Fourth Edition
Nested Selection Structures (continued) Figure 5-10: Display Message button’s Click event with the nested selection structure in the false path Programming with Microsoft Visual Basic 2008, Fourth Edition
Logic Errors in Selection Structures • Common logic errors include: • Using compound condition instead of nested structure • Reversing primary and secondary decisions • Using unnecessary nested selection structure • Algorithm: Set of step-by-step instructions for accomplishing task • Desk-checking (or hand-tracing): • Using pencil and paper to follow algorithm’s steps, using sample data • Goal is to verify that algorithm is correct Programming with Microsoft Visual Basic 2008, Fourth Edition
Logic Errors in Selection Structures (continued) Figure 5-11: A correct algorithm for the bonus procedure Programming with Microsoft Visual Basic 2008, Fourth Edition
Logic Errors in Selection Structures (continued) Figure 5-12: Results of desk-checking the correct algorithm Programming with Microsoft Visual Basic 2008, Fourth Edition
Using a Compound Condition Rather Than a Nested Selection Structure • Bonus scenario: • Calculate bonus amount for all employee types • Give extra bonus to X contingent on sales amount • Display the bonus amount • Problem when using compound condition: It does not indicate hierarchy in decision process • Nested selection structure does enforce hierarchy of decisions • Extra bonus decision depends on sales type decision Programming with Microsoft Visual Basic 2008, Fourth Edition
Using a Compound Condition Rather Than a Nested Selection Structure (continued) Figure 5-13: Correct algorithm and an incorrect algorithm containing the first logic error Programming with Microsoft Visual Basic 2008, Fourth Edition
Using a Compound Condition Rather Than a Nested Selection Structure (continued) Figure 5-14: Results of desk-checking the incorrect algorithm shown in Figure 5-13 Programming with Microsoft Visual Basic 2008, Fourth Edition
Reversing the Primary and Secondary Decisions • Incorrect algorithm reverses primary and secondary decisions • All salespeople with sales >= 10000 get extra bonus • Amount of bonus depends on sales type • Extra bonus decision should depend on sales type decision first • Reminder: Desk-check your algorithm with sample data to verify its correctness Programming with Microsoft Visual Basic 2008, Fourth Edition
Reversing the Primary and Secondary Decisions (continued) Figure 5-15: Correct algorithm and an incorrect algorithm containing the second logic error Programming with Microsoft Visual Basic 2008, Fourth Edition
Reversing the Primary and Secondary Decisions (continued) Figure 5-16: Results of desk-checking the incorrect algorithm shown in Figure 5-15 Programming with Microsoft Visual Basic 2008, Fourth Edition
Using an Unnecessary Nested Selection Structure • Problem with incorrect algorithm: • Adds extra, redundant selection structure on inner false path • Extra code reduces efficiency and readability • The redundant selection structure makes decision that was already decided in prior selection structure • Solution: Eliminate second nested structure Programming with Microsoft Visual Basic 2008, Fourth Edition
Using an Unnecessary Nested Selection Structure (continued) Figure 5-17: Correct algorithm and an incorrect algorithm containing the third logic error Programming with Microsoft Visual Basic 2008, Fourth Edition
Using an Unnecessary Nested Selection Structure (continued) Figure 5-18: Results of desk-checking the inefficient algorithm shown in Figure 5-17 Programming with Microsoft Visual Basic 2008, Fourth Edition
Multiple-Path Selection Structures • Multiple-path (or extended) selection structures: • Designed to choose from several alternatives • Condition in multiple-path selection structure does not require true/false answer • Requires expression whose value determines which path is chosen • Example: Display message based on grades ranging from A to F Programming with Microsoft Visual Basic 2008, Fourth Edition
Multiple-Path Selection Structures (continued) Figure 5-19: Letter grades and messages Programming with Microsoft Visual Basic 2008, Fourth Edition
Multiple-Path Selection Structures (continued) Figure 5-20: Pseudocode and flowchart showing a multiple-path selection structure Programming with Microsoft Visual Basic 2008, Fourth Edition
The If/ElseIf/Else Form of the Selection Structure • Allows for selection among more than two paths • Provides more convenient way of writing multiple-path selection structure than using nested If/Else structures Programming with Microsoft Visual Basic 2008, Fourth Edition
The If/ElseIf/Else Form of the Selection Structure (continued) Figure 5-21: Two versions of the code for the multiple-path selection structure Programming with Microsoft Visual Basic 2008, Fourth Edition
The Case Selection Structure • Simpler and clearer when there are many paths • Select Case statement: • Begins with Select Case, ends with End Select • Each case represents a different instruction path • Optional Case Else clause handles all values not covered by other Case paths • selectorExpression is evaluated to determine path • Each case, except Case Else, has expressionList that is compared to selectorExpression Programming with Microsoft Visual Basic 2008, Fourth Edition
The Case Selection Structure (continued) Figure 5-23: Syntax and an example of the Select Case statement Programming with Microsoft Visual Basic 2008, Fourth Edition
The Case Selection Structure (continued) Figure 5-23: Syntax and an example of the Select Case statement (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition
Specifying a Range of Values in an ExpressionList • To specify range of minimum and maximum values, use: • To keyword: Use if both upper and lower bounds are known • Is keyword: Use if only upper or lower bound is known • Example with To: Case 1 To 5 • Example with Is: Case Is > 10 • Relational operators used with Is: =, <, <=, >, >=, <> Programming with Microsoft Visual Basic 2008, Fourth Edition
Specifying a Range of Values in an ExpressionList (continued) Figure 5-24: Example of using the To and Is keywords in a Select Case statement Programming with Microsoft Visual Basic 2008, Fourth Edition
Lesson A Summary • Nested selection structures lies on true or false path of outer selection structure • Desk-check: Validate algorithm by hand with sample data • Multiple-path selection structures: Use If/ElseIf/Else statement or Select Case statement • Use To and Is keywords to specify range of values in Case clause of Select Case statement Programming with Microsoft Visual Basic 2008, Fourth Edition
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 • Create and call an independent Sub procedure • Generate random numbers Programming with Microsoft Visual Basic 2008, Fourth Edition
Creating the Math Practice Application • Objective: Practice addition and subtraction • Specifications: • 1st grade students use numbers 1 through 10 • 2nd grade students use numbers 10 through 99 • Should not allow problems whose answers will be negative numbers • Students should be able to check their answers • Extra attempts allowed when answer is incorrect • Application should track and display number of correct and incorrect responses Programming with Microsoft Visual Basic 2008, Fourth Edition
Adding a Radio Button to the Interface • Radio Button control: • Limits user to one choice in group of options • Should be labeled so its purpose is understood • Should have unique keyboard access key • RadioButton tool: Used to add radio button • Default radio button: Button initially selected • Each group of radio buttons must be placed within container such as group box • Checked property: Set to True to designate default radio button Programming with Microsoft Visual Basic 2008, Fourth Edition
Adding a Radio Button to the Interface (continued) Figure 5-27 Subtraction radio button added to the Operation group box Programming with Microsoft Visual Basic 2008, Fourth Edition
Adding a Check Box Control to the Interface • Check box control: • Allows multiple selections among group of choices • Any number of check boxes can be selected at once • Does not limit choices like radio button control • Should be labeled to indicate its purpose • Checkbox tool: Used to add check box control • Lock controls, then set TabIndex after interface design is complete Programming with Microsoft Visual Basic 2008, Fourth Edition
Adding a Check Box Control to the Interface (continued) Figure 5-28: Display summary check box added to the interface Programming with Microsoft Visual Basic 2008, Fourth Edition
Coding the Math Practice Application • Procedures to code based on TOE chart: • Click event procedures for seven controls • Load event for form • One task will be coded with an independent Sub procedure • Generating and displaying two random numbers Programming with Microsoft Visual Basic 2008, Fourth Edition
Creating an Independent Sub Procedure • Event procedure: Sub procedure associated with specific object and event • Processed when specific event occurs • Independent Sub procedure: Processed only when called (invoked) from code • Can be invoked from one or more places in an application • Not associated with specific control • Reduces amount of code, promotes modularity Programming with Microsoft Visual Basic 2008, Fourth Edition
Creating an Independent Sub Procedure (continued) Figure 5-31: Syntax and steps for creating an independent Sub procedure Programming with Microsoft Visual Basic 2008, Fourth Edition
Creating an Independent Sub Procedure (continued) Figure 5-32: Pseudocode for the GenerateAndDisplayIntegers procedure Programming with Microsoft Visual Basic 2008, Fourth Edition
Generating Random Integers • Pseudo-random number generator: • Device that produces sequence of pseudo-random numbers • Pseudo-random: Values statistically resemble randomness • To use pseudo-random number generator: • Create Random object within procedure • Generate random integers with Random.Next method Programming with Microsoft Visual Basic 2008, Fourth Edition
Generating Random Integers (continued) Figure 5-33: Syntax and examples of generating random integers Programming with Microsoft Visual Basic 2008, Fourth Edition
Generating Random Integers (continued) Figure 5-33: Syntax and examples of generating random integers (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition
Generating Random Integers (continued) Figure 5-34: Random number generation code entered in the procedure Programming with Microsoft Visual Basic 2008, Fourth Edition