Title: Chapter 4: The Selection Structure
1Chapter 4 The Selection Structure
Programming with Microsoft Visual Basic 2005,
Third Edition
2The IfThenElse Statement Lesson A Objectives
- Write pseudocode for the selection structure
- Create a flowchart to help you plan an
applications code - Write an If...Then...Else statement
- Write code that uses comparison operators and
logical operators
2
Programming with Microsoft Visual Basic 2005,
Third Edition
3The IfThenElse Statement Lesson A Objectives
(continued)?
- Change the case of a string
- Determine whether a text box contains data
- Determine the success of the TryParse method
3
Programming with Microsoft Visual Basic 2005,
Third Edition
4The Selection Structure
- Condition expression evaluating to true or false
- Selection (decision) structure
- Chooses one of two paths based on a condition
- Example
- If employee works over 40 hours, add overtime pay
- Four selection structures in Visual Basic
- If, If/Else, If/ElseIf/Else, and Case
4
Programming with Microsoft Visual Basic 2005,
Third Edition
5Writing Pseudocode for If and If/Else Selection
Structures
- If selection structure
- Contains only one set of instructions
- Instructions are processed if the condition is
true - If/Else selection
- Contains two sets of instructions
- True path instruction set following true
condition - False path instruction set following false
condition
5
Programming with Microsoft Visual Basic 2005,
Third Edition
6Writing Pseudocode for If and If/Else Selection
Structures
Figure 4-4 Examples of the If and If/Else
selection structures written in pseudocode
6
Programming with Microsoft Visual Basic 2005,
Third Edition
7Flowcharting the If and If/Else Selection
Structures
- Flowchart
- Set of standardized symbols showing program flow
- Oval the start/stop symbol
- Rectangle the process symbol
- Parallelogram the input/output symbol
- Diamond selection/repetition symbol
7
Programming with Microsoft Visual Basic 2005,
Third Edition
8Flowcharting the If and If/Else Selection
Structures (continued)?
Figure 4-5 Examples of the If and If/Else
selection structures drawn in flowchart form
8
Programming with Microsoft Visual Basic 2005,
Third Edition
9Coding the If and If/Else Selection Structures
- Syntax
- If condition Then statement block for true
path Else statement block for
false path End If - condition must be a Boolean expression
- The Else clause is optional
9
Programming with Microsoft Visual Basic 2005,
Third Edition
10Comparison Operators
- Comparison (relational) operators
- Test two items for equality or types of
non-equality - Rules for comparison operators
- Cause an expression to evaluate to true or false
- Have lower precedence than arithmetic operators
- Are evaluated from left to right
- Example 5 -2 gt 1 2 ? 3 gt 3 ? False
10
Programming with Microsoft Visual Basic 2005,
Third Edition
11Comparison Operators (continued)?
Figure 4-7 Listing and examples of commonly used
comparison operators (continued)?
11
Programming with Microsoft Visual Basic 2005,
Third Edition
12Comparison Operators (continued)?
Figure 4-7 Listing and examples of commonly used
comparison operators
12
Programming with Microsoft Visual Basic 2005,
Third Edition
13Using Comparison OperatorSwapping Numeric Values
Figure 4-11 The If selection structure shown in
the xDisplayButtons Click event procedure
13
Programming with Microsoft Visual Basic 2005,
Third Edition
14Using Comparison OperatorsDisplaying the Sum or
Difference
Figure 4-16 The If/Else selection structure
shown in the xCalcButtons Click event procedure
14
Programming with Microsoft Visual Basic 2005,
Third Edition
15Logical Operators
- Logical (Boolean) operators
- Operators that create compound conditions
- Types And, Or, Not, AndAlso, OrElse, Xor
- Example If hours gt 0 And hours lt 40 Then
- Truth tables
- Short circuit evaluation
- Operators using technique AndAlso, OrElse
15
Programming with Microsoft Visual Basic 2005,
Third Edition
16Logical Operators (continued)?
Figure 4-18 Listing and examples of logical
operators (partial)?
16
Programming with Microsoft Visual Basic 2005,
Third Edition
17Logical Operators (continued)?
Figure 4-19 Truth tables for the logical
operators (continued)?
17
Programming with Microsoft Visual Basic 2005,
Third Edition
18Logical Operators (continued)?
Figure 4-19 Truth tables for the logical
operators
18
Programming with Microsoft Visual Basic 2005,
Third Edition
19Using the Truth Tables
- Scenario calculate a bonus for a salesperson
- Bonus condition A rating and sales gt 10,000
- Appropriate operators And, AndAlso (more
efficient)? - Both conditions must be true to receive bonus
- Sample code rating "A" AndAlso sales gt 10000
- Precedence of logical operators
- Lower than that of arithmetic or comparison
operators
19
Programming with Microsoft Visual Basic 2005,
Third Edition
20Using Truth Tables (continued)?
Figure 4-20 Order of precedence for arithmetic,
comparison, and logical operators
20
Programming with Microsoft Visual Basic 2005,
Third Edition
21Using Logical Operators Calculating Gross Pay
Figure 4-22 AndAlso and OrElse logical operators
in the If...Then...Else statement (continued)?
21
Programming with Microsoft Visual Basic 2005,
Third Edition
22Using Logical Operators Calculating Gross Pay
(continued)?
Figure 4-22 AndAlso and OrElse logical operators
in the If...Then...Else statement
22
Programming with Microsoft Visual Basic 2005,
Third Edition
23Comparing Strings Containing Letters
- Scenario
- Display Pass if P is entered in
xLetterTextBox - Display Fail if F is entered in
xLetterTextBox - One of the possible implementations
- Dim letter As String
letter Me.xLetterTextBox.Text
If letter
"P" OrElse letter "p Then
Me.xResultLabel.Text Pass Else
Me.xResultLabel.Text
"Fail End if
23
Programming with Microsoft Visual Basic 2005,
Third Edition
24Converting a String to Uppercase or Lowercase
- String comparisons are case sensitive
- ToUpper method converts string to upper case
- ToLower method converts string to lower case
- Example If letter.ToUpper() "P" Then
24
Programming with Microsoft Visual Basic 2005,
Third Edition
25Comparing Boolean Values
Figure 4-29 Syntax and examples of the
String.IsNullOrEmpty method
25
Programming with Microsoft Visual Basic 2005,
Third Edition
26Comparing Boolean Values (continued)?
Figure 4-31 Syntax and an example of using the
Boolean value returned by the TryParse method
26
Programming with Microsoft Visual Basic 2005,
Third Edition
27Summary Lesson A
- A Boolean condition evaluates to true or false
- Selection structures choose an instruction path
based on a condition - If...Then...Else statement selection structure
with a true path and a false path - Operator precedence arithmetic, comparison,
logical - ToUpper and ToLower modify case of input text
27
Programming with Microsoft Visual Basic 2005,
Third Edition
28The Monthly Payment Calculator ApplicationLesson
B Objectives
- Group objects using a GroupBox control
- Calculate a periodic payment using the
Financial.Pmt method - Create a message box using the MessageBox.Show
method - Determine the value returned by a message box
28
Programming with Microsoft Visual Basic 2005,
Third Edition
29Monthly Payment Calculator
Figure 4-34 Sketch of the Monthly Payment
Calculator user interface
29
Programming with Microsoft Visual Basic 2005,
Third Edition
30Coding the Monthly Payment Calculator Application
- Procedures required according to TOE chart
- Click event code for the two buttons
- TextChanged, KeyPress, Enter code for text boxes
- Procedures that are already coded
- xExitButtons Click event and TextChanged events
- Procedure to code in Lesson B
- xCalcButtons Click event procedure
30
Programming with Microsoft Visual Basic 2005,
Third Edition
31Coding the xCalcPayButton Click Event Procedure
Figure 4-39 Pseudocode for the xCalcButtons
Click event procedure
31
Programming with Microsoft Visual Basic 2005,
Third Edition
32Using the Financial.Pmt Method
- Calculates periodic payment on loan or investment
- Syntax Financial.Pmt(Rate, NPer, PV, FV, Due)?
- Rate interest rate per period
- NPer total number of payment periods (the term)?
- PV present value of the loan or investment
- FV future value of the loan or investment
- Due due date of payments
32
Programming with Microsoft Visual Basic 2005,
Third Edition
33The MessageBox.Show Method
- Displays message box with text, button(s), icon
- Syntax MessageBox.Show(text, caption, buttons,
icon, defaultButton)? - text text to display in the message box
- caption text to display in title bar of message
box - buttons buttons to display in the message box
- icon icon to display in the message box
- defaultButton automatically selected if Enter
pressed
33
Programming with Microsoft Visual Basic 2005,
Third Edition
34The MessageBox.Show Method (continued)?
Figure 4-48 Completed xCalcButtons Click event
procedure
34
Programming with Microsoft Visual Basic 2005,
Third Edition
35The MessageBox.Show Method (continued)?
Figure 4-50 Message box created by the
MessageBox.Show method
35
Programming with Microsoft Visual Basic 2005,
Third Edition
36Summary Lesson B
- Group box control treats components as one unit
- Add a group box using the GroupBox tool
- Financial.Pmt method calculates loan or
investment payments - MessageBox.Show method displays a message box
with text, one or more buttons, and an icon
36
Programming with Microsoft Visual Basic 2005,
Third Edition
37Completing the Monthly Payment Calculator
ApplicationLesson C Objectives
- Specify the keys that a text box will accept
- Select the existing text in a text box
37
Programming with Microsoft Visual Basic 2005,
Third Edition
38Coding the KeyPress Event Procedures
- KeyPress event
- Occurs when key pressed while a control has focus
- Character corresponding to key is stored
- Stored value sent to KeyPress events e parameter
- One popular use of the KeyPress event
- Prevents users from entering inappropriate
characters - Selection structure used to test entered
character
38
Programming with Microsoft Visual Basic 2005,
Third Edition
39Coding the KeyPress Event Procedures (continued)?
Figure 4-54 Completed CancelKeys procedure
39
Programming with Microsoft Visual Basic 2005,
Third Edition
40Coding the Enter Event Procedure
- Enter event
- Occurs when the text box receives the focus
- Responsible for selecting contents of text box
- User can replace existing text by pressing a key
- SelectAll method syntax Me.textbox.SelectAll()?
- Selects all text contained on a text box
- Using the SelectAll method
- Add to each text boxs Enter event procedure
40
Programming with Microsoft Visual Basic 2005,
Third Edition
41Coding the Enter Event Procedure (continued)
Figure 4-57 Existing text selected in the
xPrincipalTextBox
41
Programming with Microsoft Visual Basic 2005,
Third Edition
42Summary Lesson C
- KeyPress event procedure responds to the user
pressing a key - One use of a KeyPress event cancel a key entered
by the user - Enter event occurs when text box receives focus
- SelectAll method used to select all contents of
a text box
42
Programming with Microsoft Visual Basic 2005,
Third Edition