Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION...
-
Upload
polly-dawson -
Category
Documents
-
view
218 -
download
1
Transcript of Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION...
![Page 1: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/1.jpg)
Programming with Microsoft Visual Basic 2010 5th Edition
CHAPTER FIVE
MORE ON THE SELECTION STRUCTURE
![Page 2: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/2.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Previewing the Math Practice Application
Open Math.exe file
The Math Practice application will be used by 1st and 2nd grade students
2
![Page 3: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/3.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Previewing the Math Practice Application (cont’d.)
3
Figure 5-1 Math Practice application’s user interface
![Page 4: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/4.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Lesson A ObjectivesAfter studying Lesson A, you should be able to: Include a nested selection structure in pseudocode and in a flowchartCode a nested selection structureDesk-check an algorithmRecognize common logic errors in selection structures
4
![Page 5: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/5.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Lesson A Objectives (cont’d.)
Include a multiple-alternative selection structure in pseudocode and in a flowchart
Code a multiple-alternative selection structure
5
![Page 6: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/6.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Nested Selection Structures
Selection structure◦ Used to make a decision and select the appropriate path
Nested selection structure◦ Selection structure that lies on true or false path of an outer selection structure
Two decisions are involved◦ Primary decision: Made by outer structure◦ Secondary decision: Made by inner structure
6
![Page 7: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/7.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition7
Figure 5-3 Selection structures you might use today
![Page 8: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/8.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
The Voter Eligibility Application
8
Figure 5-4 Problem specification for the Voter Eligibility application
![Page 9: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/9.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
The Voter Eligibility Application (cont’d.)
9
Figure 5-5 Sample run of the Voter Eligibility application
![Page 10: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/10.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition10
Figure 5-6 Flowchart showing the nested selection structure in the true path
![Page 11: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/11.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition11
Figure 5-7 Flowchart showing the nested selection structure in the false path
![Page 12: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/12.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition12
Figure 5-8 Code for the flowcharts in Figures 5-6 and 5-7 (continues)
![Page 13: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/13.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition13
Figure 5-8 Code for the flowcharts in
Figures 5-6 and 5-7 (cont’d.)
![Page 14: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/14.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Logic Errors in Selection Structures
Common logic errors◦ 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 a task
14
![Page 15: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/15.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Logic Errors in Selection Structures (cont’d.)
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
15
![Page 16: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/16.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Logic Errors in Selection Structures (cont’d.)
16
Figure 5-9 A correct algorithm for the bonus procedure
![Page 17: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/17.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Logic Errors in Selection Structures (cont’d.)
17
Figure 5-10 Results of desk-checking the correct algorithm
![Page 18: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/18.jpg)
Programming with Microsoft Visual Basic 2010, 5th 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
18
![Page 19: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/19.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Figure 5-11 Correct algorithm and an incorrect algorithm containing the first logic error
19
![Page 20: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/20.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Figure 5-12 Results of desk-checking the incorrect algorithm shown in Figure 5-11
20
![Page 21: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/21.jpg)
Programming with Microsoft Visual Basic 2010, 5th 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
21
![Page 22: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/22.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Figure 5-13 Correct algorithm and an incorrect algorithm containing the second logic error
22
![Page 23: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/23.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Reversing the Primary and Secondary Decisions (cont’d.)
23
Figure 5-14 Results of desk-checking the incorrect algorithm shown in Figure 5-13
![Page 24: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/24.jpg)
Programming with Microsoft Visual Basic 2010, 5th 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
24
![Page 25: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/25.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Using an Unnecessary Nested Selection Structure (cont’d.)
25
Figure 5-15 Correct algorithm and an incorrect algorithm containing the third logic error
![Page 26: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/26.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Using an Unnecessary Nested Selection Structure (cont’d.)
26
Figure 5-16 Results of desk-checking the inefficient algorithm shown in Figure 5-15
![Page 27: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/27.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Multiple-Alternative Selection Structures
Multiple-alternative selection structures◦ Designed to choose from several alternatives
Condition in multiple-alternative 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
27
![Page 28: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/28.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Multiple-Alternative Selection Structures (cont’d.)
28
Figure 5-17 Letter grades and messages
![Page 29: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/29.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Figure 5-18 Pseudocode containing a multiple-alternative selection structure
29
![Page 30: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/30.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Figure 5-19 Flowchart containing a multiple-alternative selection structure
30
![Page 31: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/31.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Multiple-Alternative Selection Structures (cont’d.)
Figure 5-20◦ Shows two versions of code for btnDisplay control’s Click event procedure◦ Both versions use If…Then…Else statements◦ Second version contains more convenient way of writing a multiple-
alternative selection structure
31
![Page 32: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/32.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Figure 5-20 Two versions of the code containing a multiple-alternative selection structure (continues)
32
![Page 33: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/33.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Figure 5-20 Two versions of the code containing a multiple-alternative selection structure (cont’d.)
33
![Page 34: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/34.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
The Select Case Statement
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
34
![Page 35: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/35.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Figure 5-22 Syntax and an example of the Select Case statement (continues)
35
![Page 36: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/36.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Figure 5-22 Syntax and an example of the Select Case statement (cont’d.)
36
![Page 37: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/37.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Specifying a Range of Values in a Case Clause
To specify range of minimum and maximum values◦ 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
Comparison operators used with Is = , < , <= , > , >= , <>
37
![Page 38: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/38.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Figure 5-23 Syntax and an example of specifying a range of values
38
![Page 39: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/39.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Lesson A Summary Nested selection structure lies on true or false path of outer selection structure
Desk-check◦ Validate algorithm by hand with sample data
Multiple-alternative 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
39
![Page 40: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/40.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Lesson B ObjectivesAfter 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
40
![Page 41: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/41.jpg)
Programming with Microsoft Visual Basic 2010, 5th 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
41
![Page 42: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/42.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Adding a Radio Button to the Form
Radio Button◦ 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
Each group of radio buttons must be placed within container such as group box
Default radio button: Button initially selected ◦ Set Checked property to True
42
![Page 43: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/43.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Adding a Radio Button to the Form (cont’d.)
43
Figure 5-29 Subtraction radio button added to the Operation group box
![Page 44: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/44.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Adding a Check Box Control to the Form
Check box◦ 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
44
![Page 45: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/45.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Adding a Check Box Control to the Form (cont’d.)
45
Figure 5-30 Display summary check box added to the form
![Page 46: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/46.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Coding the Math Practice Application
Procedures to code based on TOE chart ◦ Form’s load event procedure◦ Click event procedures for seven controls
One task will be coded with an independent Sub procedure◦ Generating and displaying two random numbers
46
![Page 47: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/47.jpg)
Programming with Microsoft Visual Basic 2010, 5th 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
47
![Page 48: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/48.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Figure 5-33 Independent Sub procedure syntax, example, and steps
48
![Page 49: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/49.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Figure 5-34 Pseudocode for the GenerateAndDisplayIntegers procedure
49
![Page 50: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/50.jpg)
Programming with Microsoft Visual Basic 2010, 5th 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
50
![Page 51: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/51.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Figure 5-35 Syntax and examples of generating random integers
51
![Page 52: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/52.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Figure 5-36 Random number generation code entered in the procedure
52
![Page 53: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/53.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Figure 5-37 Additional comments and code entered in the procedure
53
![Page 54: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/54.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Coding the Grade Radio Buttons’ Click Event Procedures
Radio button controls should invoke GenerateAndDisplayNumbers procedure
Call statement: Invokes independent procedure
Syntax Call procedureName([argumentList])◦ procedureName: Name of procedure to be invoked◦ argumentList: Optional list of arguments to pass
Keyword Call is optional◦ Can simply use procedure name
54
![Page 55: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/55.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Figure 5-38 Syntax and examples of the Call statement
55
![Page 56: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/56.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Coding the Operation Radio Buttons’ Click Event Procedures
Requirements for Click event procedures◦ Display appropriate mathematical operator (+ or -) ◦ Generate and display two random integers◦ Show addition problem by default when program starts
56
![Page 57: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/57.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Coding the Form’s Load Event ProcedureForm’s Load event procedure
Processed when application is started Form is not displayed until its instructions are processed
Two ways to initially display addition problem Call GenerateAndDisplayIntegers() radAddition.PerformClick()
Enter either statement in Load event procedure
57
![Page 58: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/58.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Lesson B Summary Radio button control
◦ Limits user to only one choice among group of choices
Check box control◦ Allows user to select multiple choices in a group
Independent Sub procedures◦ Not linked to controls and can be invoked anywhere in program
Use Random object to generate pseudo-random numbers
58
![Page 59: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/59.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Lesson B Summary (cont’d.)
Call statement◦ Used to call (invoke) independent Sub procedure
Use object.PerformClick() to invoke object’s Click event
59
![Page 60: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/60.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Lesson C ObjectivesAfter studying Lesson C, you should be able to: Code a check box’s Click event procedureShow and hide a control
60
![Page 61: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/61.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Coding the Check Answer Button’s Click Event Procedure
• Study procedure’s pseudocode to determine what variables and named constants are neededSix integer variables and one named constant are neededTwo variables have static storage type
61
![Page 62: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/62.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Figure 5-44 Pseudocode for the btnCheckAnswer control’s Click event procedure
62
![Page 63: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/63.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Figure 5-45 Memory locations used by the btnCheckAnswer control’s Click event procedure
63
![Page 64: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/64.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Coding the Display Summary Check Box’s Click Event Procedure
chkSummary control’s Click event procedure◦ Displays and hides grpSummary control◦ When user selects check box, event is triggered, showing group box and its
contents◦ When user deselects check box, event is triggered, hiding group box control
Checked property indicates whether check box is selected
Visible property indicates whether control is shown
64
![Page 65: Programming with Microsoft Visual Basic 2010 5 th Edition CHAPTER FIVE MORE ON THE SELECTION STRUCTURE.](https://reader036.fdocuments.us/reader036/viewer/2022062321/56649f335503460f94c4ff8c/html5/thumbnails/65.jpg)
Programming with Microsoft Visual Basic 2010, 5th Edition
Lesson C Summary To show or hide control, set control’s Visible property
Use selection structure to determine if check box was either selected or deselected by user
65