Professional Documents
Culture Documents
Chapter Four
Making Decisions in a Program
Objectives
After studying this chapter, you should be able to:
• Include the selection structure in pseudocode and in
a flowchart
• Explain the difference between single-alternative and
dual-alternative selection structures
• Code a selection structure using the If…Then…Else
statement
• Include comparison and logical operators in a
selection structure’s condition
• Prevent the division by zero run time error 2
6
Figure 4-1: Single-alternative and dual-alternative selection structures
7
Figure 4-2: Rosebud Roses application (sequence structure only)
11
Figure 4-6: How to use the If…Then…Else statement (continues)
13
Figure 4-7 How to use comparison operators in a condition
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Comparison Operators (cont’d.)
14
Figure 4-8 How to evaluate expressions containing arithmetic and comparison operators
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Comparison Operators (cont’d.)
Preventing the
Division by Zero Error
• A run time error occurs
when an expression
attempts to divide a value
by the number 0
• In math, division by zero
is not allowed
• Prevent the run time
error from occurring by
using a selection
structure whose
condition compares the
expression’s
15
denominator with the
number 0 Figure 4-9: How to prevent the division by zero run time error
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Swapping Numeric Values
16
Microsoft Visual Basic 2012: Reloaded, Fifth Edition Figure 4-10: Auction House application
Swapping Numeric Values (cont’d.)
18
Figure 4-12: How to swap the contents of two variables
19
Figure 4-13: How to
concatenate strings
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Comparing Strings
•Use the ToUpper method to
temporarily convert a string
to uppercase
•Use the ToLower method to
temporarily convert a string
to lowercase
•A text box’s CharacterCasing
property indicates whether
the text inside the control
should remain as typed, or
be converted to either
uppercase or lowercase 20
Figure 4-14: How to use the ToUpper and ToLower
Microsoft Visual Basic 2012: Reloaded, Fifth Edition methods
The Math Calculator Application
Figure 4-15:
Math
Calculator
21
application
(continues)
Figure 4-15:
Math
Calculator
22
application
23
Figure 4-16: Two versions of the calcButton_Click procedure
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Comparing Boolean Values
• Check boxes provide one
or more independent
and nonexclusive items
from which the user can
choose
• An interface can contain
any number of check
boxes, and any number Figure 4-17: Sample run of the Halloway Products
of them can be selected application
25
26
Figure 4-19: Examples of the English logical operators
27
28
30
31
32
Figure 4-23: Two versions of the Calculate button’s Click event procedure
33
34
36
Microsoft Visual Basic 2012: Reloaded, Fifth Edition Figure 4-25: How to generate random integers
Generating Random Integers
(cont’d.)
37
Figure 4-26: Sample run and code for the Random Integer application
39
Figure 4-37: Sample run of the application
40
Figure 4-40: User Interface
44
46