Title: Chapter 5: More on the Selection Structure
1Chapter 5 More on the Selection Structure
Programming with Microsoft Visual Basic 2005,
Third Edition
2Nested, If/ElseIf/Else, and Case Selection
StructuresLesson A Objectives
- 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
2
Programming with Microsoft Visual Basic 2005,
Third Edition
3Nested, If/ElseIf/Else, and Case Selection
StructuresLesson A Objectives (continued)?
- Code an If/ElseIf/Else selection structure
- Include a Case selection structure in pseudocode
and in a flowchart - Code a Case selection structure
- Write code that uses the Is, TypeOfIs, and Like
comparison operators
3
Programming with Microsoft Visual Basic 2005,
Third Edition
4Negation of boolean expression
- Not (a And b) ? (Not a) Or (Not b)?
- Not (a Or b) ? (Not a) And (Not b)?
- ? is equivalent
- You can replace And and Or with AndAlso and
OrElse. - Verify these equations by using the truth tables
5Previewing the Completed Application
(VB2005\Chap05)?
Figure 5-1 Math Practice applications user
interface
5
Programming with Microsoft Visual Basic 2005,
Third Edition
6Nested Selection Structures
Figure 5-3 Pseudocode showing the nested
selection structure
6
Programming with Microsoft Visual Basic 2005,
Third Edition
7Nested Selection Structures (continued)?
Figure 5-4 Flowchart showing the nested
selection structure
7
Programming with Microsoft Visual Basic 2005,
Third Edition
8Logic Errors in Selection Structures
- Common logic errors with selection structures
- Using a logical operator instead of nested
structure - Reversing the primary and secondary decisions
- Using an unnecessary nested selection structure
- Algorithm
- Desk-checking, hand-tracing, run by hand
8
Programming with Microsoft Visual Basic 2005,
Third Edition
9Logic Errors in Selection Structures (continued)?
codesalesperson code. Only salespeople with code
x will get the bonus.
Figure 5-10 A correct algorithm for the bonus
procedure
9
Programming with Microsoft Visual Basic 2005,
Third Edition
10Logic Errors in Selection Structures (continued)?
Figure 5-11 Results of desk-checking the correct
algorithm shown in Figure 5-10
10
Programming with Microsoft Visual Basic 2005,
Third Edition
11Using a Logical Operator Rather Than a Nested
Selection Structure (continued)?
Figure 5-11 Correct algorithm and an incorrect
algorithm containing the first logic error
11
Programming with Microsoft Visual Basic 2005,
Third Edition
12Reversing the Primary and Secondary Decisions
(continued)?
Figure 5-14 Correct algorithm and an incorrect
algorithm containing the second logic error
12
Programming with Microsoft Visual Basic 2005,
Third Edition
13Using an Unnecessary Nested Selection Structure
(continued)?
Figure 5-16 Correct algorithm and an inefficient
algorithm containing the third logic error
13
Programming with Microsoft Visual Basic 2005,
Third Edition
14The If/ElseIf/Else Selection Structure
- Extended (multiple path) selection structures
- If/ElseIf/Else
- Select Case
- If/ElseIf/Else
- Allows for selection among more than two paths
- Provides a more readable version of selection
logic - Example display message based on grades A F
14
Programming with Microsoft Visual Basic 2005,
Third Edition
15The If/ElseIf/Else Selection Structure
(continued)?
Figure 5-18 Two versions of xDisplayMsgButtons
Click event procedure (continued)?
15
Programming with Microsoft Visual Basic 2005,
Third Edition
16The If/ElseIf/Else Selection Structure
(continued)?
Figure 5-18 Two versions of xDisplayMsgButtons
Click event procedure
16
Programming with Microsoft Visual Basic 2005,
Third Edition
17The Case Selection Structure
- Alternative to If/ElseIf/Else multiple path
selection - Select Case statement
- Begins with Select Case, ends with End Select
clause - Each Case represents a different instruction path
- Optional Case Else clause is the default case
- selectorExpression is evaluated to determine path
- Each case, except Case Else, has an
expressionList - How the computer determines the instruction path
- selectorExpression value matches expressionList
value
17
Programming with Microsoft Visual Basic 2005,
Third Edition
18The Case Selection Structure (continued)?
short-circuiting
Figure 5-22 Syntax and an example of the Select
Case statement (continued)?
18
Programming with Microsoft Visual Basic 2005,
Third Edition
19The Case Selection Structure (continued)?
Figure 5-21 Flowchart showing the Case selection
structure
19
Programming with Microsoft Visual Basic 2005,
Third Edition
20The Case Selection Structure (continued)?
Figure 5-22 Syntax and an example of the Select
Case statement
20
Programming with Microsoft Visual Basic 2005,
Third Edition
21Desk-Checking the xDisplayMsgButtons Click Event
Procedure
- Desk check with the letters C, F, and X
21
Programming with Microsoft Visual Basic 2005,
Third Edition
22Desk-Checking the xDisplayMsgButtons Click Event
Procedure (continued)?
Figure 5-23 Results of desk-checking the
xDisplayMsgButtons Click event procedure shown
in Figure 5-22
22
Programming with Microsoft Visual Basic 2005,
Third Edition
23Specifying a Range of Values in an ExpressionList
- Specifying range of minimum and maximum values
- To keyword use if 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 gt 10
- Relational operators used with Is , lt, lt, gt,
gt, ltgt
23
Programming with Microsoft Visual Basic 2005,
Third Edition
24The Is, TypeOfIs, and Like Comparison Operators
Those work independent of the select statement
- Is operator
- Determines if two object references are the same
- TypeOf...Is
- Determines whether an object is a specified type
- Like
- Determines whether a string matches a given
pattern
24
Programming with Microsoft Visual Basic 2005,
Third Edition
25The Is Comparison Operator
- Is operator
- Determines if two object references are the same
- Object reference
- Memory address within computers internal memory
- Syntax objectReference1 Is objectReference2
- Evaluates to True if object references are the
same - Otherwise evaluates to False
25
Programming with Microsoft Visual Basic 2005,
Third Edition
26The TypeOfIs Comparison Operator
- TypeOfIs operator
- Determines whether an object is a specified type
- Syntax TypeOf object Is objectType
- Evaluates to True if type of object equals
objectType - Otherwise evaluates to False
26
Programming with Microsoft Visual Basic 2005,
Third Edition
27The TypeOfIs Comparison Operator (continued)?
Figure 5-29 Syntax and an example of the
TypeOf...Is operator
27
Programming with Microsoft Visual Basic 2005,
Third Edition
28The Like Comparison Operator
Figure 5-31 Syntax and examples of the Like
operator
28
Programming with Microsoft Visual Basic 2005,
Third Edition
29The Like Comparison Operator (continued)?
Figure 5-31 Syntax and examples of the Like
operator
29
Programming with Microsoft Visual Basic 2005,
Third Edition
30The Math Practice ApplicationLesson B Objectives
- 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
30
Programming with Microsoft Visual Basic 2005,
Third Edition
31The Math Practice ApplicationLesson B Objectives
(continued)?
- Generate random numbers
- Invoke a radio buttons Click event procedure
from code
31
Programming with Microsoft Visual Basic 2005,
Third Edition
32The Math Practice Application
Figure 5-39 Display summary check box added to
the interface
32
Programming with Microsoft Visual Basic 2005,
Third Edition
33Lesson C Objectives
- Code a check box's Click event procedure
- Display and hide a control