Week 5

advertisement
Programming with Microsoft
Visual Basic 2012
Chapter 5: More on the Selection Structure
Previewing the Modified Covington
Resort Application
Figure 5-1 Interface showing the calculated amounts
Programming with Microsoft Visual Basic 2012
Figure 5-2 Recalculated amounts shown in the interface
2
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
3
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
4
Nested Selection Structures
(cont.)
Figure 5-5 A problem that
requires two nested
selection structures
Programming with Microsoft Visual Basic 2012
5
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
6
Flowcharting a Nested Selection
Structure
(cont.)
Figure 5-7 Another
correct solution for the
voter eligibility problem
Programming with Microsoft Visual Basic 2012
7
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
8
Logic Errors in Selection Structures
• Four common errors:
1. Using a compound condition rather than a nested
selection structure
2. Reversing the decisions in the outer and nested
selection structures
3. Using an unnecessary nested selection structure
4. Including an unnecessary comparison in a condition
Programming with Microsoft Visual Basic 2012
9
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
10
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
11
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
12
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
13
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
14
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
15
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
16
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
17
Multiple-Alternative Selection
Structures
(cont.)
Figure 5-21 Pseudocode and flowchart containing a multiple-alternative selection structure
Programming with Microsoft Visual Basic 2012
18
Multiple-Alternative Selection
Structures
(cont.)
Figure 5-23 Excellent message
shown in the interface
Figure 5-22 Two versions of the
code containing a multiplealternative selection structure
Programming with Microsoft Visual Basic 2012
19
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
20
The Select Case Statement
(cont.)
Figure 5-24 Syntax and an example of the Select Case statement
Programming with Microsoft Visual Basic 2012
21
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
22
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
23
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 Is keyword 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
24
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
25
Modifying the Covington Resort
Application
Figure 5-36 Revised TOE chart for the Covington Resort application (continues)
Programming with Microsoft Visual Basic 2012
26
Modifying the Covington Resort
Application
(cont.)
(continued)
Figure 5-36 Revised TOE chart for the Covington Resort application
Programming with Microsoft Visual Basic 2012
27
Modifying the Covington Resort
Application
(cont.)
Figure 5-37 Partially completed interface for the Covington Resort application
Programming with Microsoft Visual Basic 2012
28
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
29
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
30
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
31
Modifying the Covington Resort
Application
(cont.)
Figure 5-40 Correct TabIndex values
Programming with Microsoft Visual Basic 2012
32
Modifying the Calculate Button’s
Code
Figure 5-41 Modified pseudocode for the btnCalc_Click procedure
Programming with Microsoft Visual Basic 2012
33
Modifying the Calculate Button’s
Code
(cont.)
Figure 5-42 Modified list of named constants and variables
Programming with Microsoft Visual Basic 2012
34
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
35
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
36
Modifying the Calculate Button’s
Code
(cont.)
(continued)
Figure 5-45 Examples of comparing Boolean values
Programming with Microsoft Visual Basic 2012
37
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
38
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
39
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
40
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
41
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
42
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
43
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
44
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
45
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
46
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
47
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
48
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
49
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
50
Using the TryParse Method for Data
Validation
(cont.)
Figure 5-60 Modified btnCalc_Click procedure
Programming with Microsoft Visual Basic 2012
51
Generating Random Integers
• Pseudo-random number generator
– A device that produces a sequence of numbers that meet
certain statistical requirements for randomness
– Create a Random object by declaring it in a Dim
statement
– Use the Random.Next method to generate random
integers
Programming with Microsoft Visual Basic 2012
52
Generating Random Integers
(cont.)
Figure 5-61 Syntax and examples of generating random integers
Programming with Microsoft Visual Basic 2012
53
Generating Random Integers
(cont.)
Figure 5-62 Roll ‘Em Game application’s interface
Figure 5-63 Pseudocode for the Roll the Dice button’s Click event procedure
Programming with Microsoft Visual Basic 2012
54
Generating Random Integers
(cont.)
Figure 5-64 Result of clicking the Roll the Dice button
Programming with Microsoft Visual Basic 2012
55
Showing and Hiding a Control
• Hide objects by changing their Visible property from
True to False
Figure 5-65 Resized form
Programming with Microsoft Visual Basic 2012
Figure 5-66 Interface with six of the picture boxes hidden
56
Showing and Hiding a Control
(cont.)
Figure 5-67 Roll the Dice button’s Click event procedure
Programming with Microsoft Visual Basic 2012
57
Lesson C Summary
• To determine whether the TryParse method converted a
string to a number of the specified data type:
– Use the syntax booleanVariable =
dataType.TryParse(string, numericVariableName)
– The TryParse method returns the Boolean value True
when the string can be converted to the numeric
dataType
– Otherwise, it returns the Boolean value False
Programming with Microsoft Visual Basic 2012
58
Lesson C Summary
(cont.)
• To generate random integers:
– Create a Random object to represent the pseudo-random
number generator
• The syntax for creating a Random object is
Dim randomObjectName As New Random
– Use the Random.Next method to generate a random integer
• The method’s syntax is randomObjectName.Next(minValue,
maxValue)
• The Random.Next method returns an integer that is greater than
or equal to minValue, but less than maxValue
• The Random.Next method’s return value is assigned to a variable
Programming with Microsoft Visual Basic 2012
59
Lesson C Summary
(cont.)
• To show or hide a control while an application is
running:
– Set the control’s Visible property to the Boolean value
True to show the control during runtime
– Set the control’s Visible property to the Boolean value
False to hide the control during runtime
Programming with Microsoft Visual Basic 2012
60
Download