You are on page 1of 46

Microsoft Visual

Basic 2012: Reloaded


Fifth Edition

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

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


Objectives (cont’d.)

• Prevent the division by zero run time error


• Swap two values
• Create a block-level variable
• Concatenate strings
• Use the ControlChars.NewLine constant
• Change the case of a string
• Include a check box in an interface
• Generate random numbers
3

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


The Selection Structure
• The Selection Structure - also called the decision structure –
indicates that a decision needs to me made before the
program continues
• The decision is based on a condition that must be evaluated to
determine the next instruction to process
• The condition must evaluate to either True or False only
Single-Alternative and Dual-Alternative Selection Structures
• A single-alternative selection structure has a specific set of
instructions to follow only when its condition evaluates to
True
• A dual-alternative selection structure has one set of
instructions to follow when the condition evaluates to True, 4

but a different set of instructions to follow when the condition


Microsoft Visual Basic 2012: Reloaded, Fifth Edition
evaluates to False
The Selection Structure (cont’d.)

Figure 4-1: Single-alternative and dual-


alternative selection structures (Continues)
5

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


The Selection Structure (cont’d.)

• The instructions to follow when the condition evaluates to


True are called the True path
• The instructions to follow when the condition evaluates to
False are called the False path

6
Figure 4-1: Single-alternative and dual-alternative selection structures

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


The Selection Structure (cont’d.)

7
Figure 4-2: Rosebud Roses application (sequence structure only)

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


The Selection Structure (cont’d.)

Figure 4-3: Modified Rosebud Roses application


(single-alternative selection structure)

Figure 4-4: Single-alternative selection structure 8


shown in a flowchart

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


The Selection Structure (cont’d.)

Figure 4-5: Modified Rosebud Roses application


(dual-alternative selection structure)

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


Coding Single-Alternative and Dual-
Alternative Selection Structures
• The If…Then…Else statement is used for coding single-
alternative and dual-alternative selection structures
• The set of statements contained in each path is referred to as
a statement block

Figure 4-6: How to use the If…


Then…Else statement
10
(continues)

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


Coding Single-Alternative and Dual-
Alternative Selection Structures (cont’d.)

11
Figure 4-6: How to use the If…Then…Else statement (continues)

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


Comparison Operators
• Each comparison operator (also referred to as relational
operators) can be used to compare two values, and the
comparison always results in a Boolean value: either True or
False.
• When comparing values, keep in mind that equal to (=) is the opposite
of not equal to (<>), greater than (>) is the opposite of less than or equal
to (<=), and less than (<) is the opposite of greater than or equal to (>=)

Figure 4-7 How to


use comparison
operators in a
condition 12
(continues)

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


Comparison Operators (cont’d.)

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.)

• Like a variable declared at the beginning of a


procedure, a variable declared within a statement
block—referred to as a block-level variable—
remains in memory until the procedure ends
• Unlike a variable declared at the beginning of a
procedure, block-level variables have block scope
rather than procedure scope
• A variable that has block scope can be used only
within the statement block in which it is declared
17

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


Swapping Numeric Values (cont’d.)

18
Figure 4-12: How to swap the contents of two variables

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


String Concatenation
• The ControlChars.NewLine constant advances the insertion
point to the next line
• Use the concatenation operator, which is the ampersand (&),
to concatenate (connect or link together) strings

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)

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


The Math Calculator Application (cont’d.)

Figure 4-15:
Math
Calculator
22
application

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


The Math Calculator Application (cont’d.)

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

at the same time


• Each check box should
be labeled to make its
24
purpose obvious
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Comparing Boolean Values

Figure 4-18: Calculate button’s Click event procedure

25

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


Logical Operators
• Logical operators allow you to combine two or more
sub-conditions into one compound condition
• The compound condition will always evaluate to a
Boolean value: either True or False

26
Figure 4-19: Examples of the English logical operators

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


Logical Operators (cont’d.)

27

Figure 4-20: How to use logical operators in a


Microsoft Visual Basic 2012: Reloaded, Fifth Edition
condition (continues)
Logical Operators (cont’d.)

28

Figure 4-20: How to use logical operators in a


Microsoft Visual Basic 2012: Reloaded, Fifth Edition
condition
Logical Operators (cont’d.)

• Truth tables summarize how the computer evaluates


expressions containing a logical operator
• Because both sub-conditions combined with the AndAlso
operator need to be True for the compound condition to be
True, there is no need to evaluate sub-condition2 when
subcondition1 is False
• Because only one of the sub-conditions combined with the
OrElse operator needs to be True for the compound
condition to be True, there is no need to evaluate sub-
condition2 when sub-condition1 is True
• The concept of evaluating sub-condition2 based on the
result of sub-condition1 is referred to as short-circuit 29
evaluation
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Logical Operators (cont’d.)

Figure 4-21: How to evaluate expressions containing a logical operator

30

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


Logical Operators (cont’d.)

• Using the Truth tables


• If orderAmount > 100 AndAlso creditCard =
“Warren”
• If orderAmount > 100 OrElse club = “Member”

31

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


The Carroll Company Application

Figure 4-22: Sample runs of the Carroll Company application

32

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


The Carroll Company Application
(cont’d.)

Figure 4-23: Two versions of the Calculate button’s Click event procedure

33

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


Summary of Operators

34

Figure 4-24: How to determine the order in which


Microsoft Visual Basic 2012: Reloaded, Fifth Edition operators are evaluated
Generating Random Integers
• Most programming languages provide a pseudo-random number
generator, which is a device that produces a sequence of
numbers that meet certain statistical requirements for
randomness
• Pseudo-random numbers are chosen with equal probability from
a finite set of numbers
• The chosen numbers are not completely random
• This is because a definite mathematical algorithm is used to select them
• First create a Random object to represent the pseudo-random
number generator in your application’s code
• After the Random object is created, you can use the object’s
Random.Next method to generate random integers
35

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


Generating Random Integers
(cont’d.)

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

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


Programming Tutorial 1
Figure 4-27: TOE chart for the Find the Robot
application

Figure 3-41: TOE chart for the


Color Mixer application

Figure 4-28MainForm for the Find the


Robot application 38

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


Programming Tutorial 2

Figure 4-34: TOE chart for the Greenview Health Club


application

39
Figure 4-37: Sample run of the application

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


Programming Example

Figure 4-39: TOE chart

40
Figure 4-40: User Interface

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


Summary
• The selection structure allows a procedure to make a decision
(based on some condition) and then take the appropriate
action
• There are three types of selection structures: single-
alternative, dual-alternative, and multiple-alternative
• The condition in a selection structure must evaluate to either
True or False
• In a single-alternative selection structure, a specific set of tasks is
performed only when the condition evaluates to True
• In a dual-alternative selection structure, one set of tasks is performed
when the condition evaluates to True, but a different set of tasks is
performed when it evaluates to False
41

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


Summary (cont'd.)

• A selection structure’s condition is represented in a


flowchart by a diamond, which is called the decision
symbol
• The decision symbol has one flowline entering the
symbol, and two flowlines (marked with a T and an F)
leaving the symbol
• Visual Basic provides the If…Then…Else statement
for coding single-alternative and dual-alternative
selection structures
• All expressions containing a comparison operator
42
evaluate to a Boolean value: either True or False
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Summary (cont'd.)

• Comparison operators do not have an order of


precedence in Visual Basic
• If an expression contains more than one comparison
operator, the comparison operators are evaluated
from left to right
• Comparison operators are evaluated after any
arithmetic operators in an expression
• Variables declared in a statement block (for
example, in the True or False path of a selection
structure) have block scope and are referred to as
block-level variables 43

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


Summary (cont'd.)

• You connect (or link) strings together using the


concatenation operator, which is the ampersand (&)
• The ControlChars.NewLine constant advances the
insertion point to the next line in a control
• String comparisons in Visual Basic are case-sensitive
• When comparing strings, you can use either the
ToUpper method or the ToLower method to
temporarily convert the strings to uppercase or
lowercase, respectively

44

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


Summary (cont'd.)

• You can use check boxes in an interface to provide


the user with one or more independent and
nonexclusive items from which to choose
• The value in a check box’s Checked property indicates
whether the check box is selected (True) or
unselected (False)
• You use logical operators to combine two or more
sub-conditions into one compound condition
• The compound condition always evaluates to a
Boolean value: either True or False
45

Microsoft Visual Basic 2012: Reloaded, Fifth Edition


Summary (cont'd.)

• The AndAlso logical operator is evaluated before the


OrElse logical operator in an expression
• Logical operators are evaluated after any arithmetic
and comparison operators in an expression
• You use the pseudo-random number generator in
Visual Basic to generate random numbers
• The pseudo-random number generator is an object
whose data type is Random

46

Microsoft Visual Basic 2012: Reloaded, Fifth Edition

You might also like