Uploaded by Elangelo M

Chapter 5

advertisement
PROGRAMMING
Selection Structure
Comparison Operators
Concatenation
Logical Operators
1
THE SELECTION STRUCTURE
• 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
• There are three types of selection structures: singlealternative, dual-alternative, and multiple-alternative
© 2016 Cengage Learning®. May not be scanned,
copied or duplicated, or posted to a publicly accessible
website, in whole or in part.
THE SELECTION STRUCTURE
(CONT’D.)
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, but a different set of instructions to follow
when the condition evaluates to False
© 2016 Cengage Learning®. May not be scanned,
copied or duplicated, or posted to a publicly accessible
website, in whole or in part.
3
THE SELECTION STRUCTURE
(CONT’D.)
• True path - the
instructions to
follow when the
condition evaluates
to True
• False path - the
instructions to
follow when the
condition evaluates
to False
Figure 4-1: Single-alternative and
dual-alternative selection structures
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible4
website, in whole or in part.
THE SELECTION STRUCTURE
(CONT’D.)
Figure 4-2: Somerset Day Spa application (sequence structure only)
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible5
website, in whole or in part.
THE SELECTION STRUCTURE
(CONT’D.)
• The diamond in a flowchart is
called the decision symbol - it is
used to represent the condition
(decision) in the selection structure
Figure 4-3: Modified Somerset Day Spa
application (single-alternative selection
structure)
© 2016 Cengage Learning®. May not be scanned,
copied or duplicated, or posted to a publicly accessible
website, in whole or in part.
6
THE SELECTION STRUCTURE
(CONT’D.)
Figure 4-4: Modified Somerset Day Spa
application (dual-alternative selection
structure)
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible7
website, in whole or in part.
THE IF…THEN…ELSE STATEMENT
 The If…Then…Else statement is used for coding singlealternative and dual-alternative selection structures
 The set of statements contained in each path is referred to as
a statement block
Figure 4-5: How to use the
If…Then…Else statement
(continues)
© 2016 Cengage Learning®. May not be scanned,
copied or duplicated, or posted to a publicly accessible
website, in whole or in part.
8
THE IF…THEN…ELSE STATEMENT
(CONT’D.)
Figure 4-5: How to use the If…Then…Else statement
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible9
website, in whole or in part.
COMPARISON OPERATORS
 Each comparison operator (also referred to as relational
operators) can be used to compare two values
 The comparison always results in a Boolean value: either True
or False
Figure 4-6 How to use comparison operators in a condition (continues)
© 2016 Cengage Learning®. May not be scanned,
copied or duplicated, or posted to a publicly accessible
website, in whole or in part.
COMPARISON OPERATORS
(CONT’D.)
Figure 4-6 How to use comparison operators in a condition
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible11
website, in whole or in part.
COMPARISON OPERATORS
(CONT’D.)
Figure 4-7 How to evaluate expressions containing arithmetic and comparison operators
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible12
website, in whole or in part.
COMPARISON OPERATORS
(CONT’D.)
Checking the Denominator in an Expression
• In math, division by zero is not allowed
• A run time error occurs when an expression
attempts to divide a value by the number 0
• Prevent the error from occurring by using a
selection structure whose condition compares
the expression’s denominator with the number 0
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible13
website, in whole or in part.
COMPARISON OPERATORS
(CONT’D.)
Figure 4-8: How to check a denominator in an expression
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible14
website, in whole or in part.
COMPARISON OPERATORS
(CONT’D.)
Swapping Numeric Values
Figure 4-9: Perry’s Auction House application (continues)
© 2016 Cengage Learning®. May not be scanned,
copied or duplicated, or posted to a publicly accessible
website, in whole or in part.
COMPARISON OPERATORS
(CONT’D.)
Figure 4-9: Perry’s Auction House application
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible16
website, in whole or in part.
COMPARISON OPERATORS
(CONT’D.)
Figure 4-10: displayButton_Click procedure
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible17
website, in whole or in part.
COMPARISON OPERATORS
(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
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible18
website, in whole or in part.
COMPARISON OPERATORS
(CONT’D.)
Figure 4-11: How to swap the contents of two variables
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible19
website, in whole or in part.
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
Figure 4-12: Assignment statement and sample run of the Perry’s Auction House
application
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible20
website, in whole or in part.
STRING CONCATENATION
(CONT.)
Figure 4-13: How to concatenate strings
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible21
website, in whole or in part.
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
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible22
website, in whole or in part.
COMPARING STRINGS
(CONT.)
Figure 4-14: How to use the ToUpper and
ToLower methods
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible23
website, in whole or in part.
COMPARING STRINGS
(CONT.)
Figure 4-15: How to compare strings that use digits and special characters
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible24
website, in whole or in part.
COMPARING STRINGS
(CONT.)
The Square Calculations
Application
Figure 4-16: Square
Calculations application
(continues)
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible25
website, in whole or in part.
COMPARING STRINGS
(CONT.)
Figure 4-16: Square
Calculations application
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible26
website, in whole or in part.
COMPARING STRINGS
(CONT.)
Figure 4-17: Two versions of the calcButton_Click procedure (continues)
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible27
website, in whole or in part.
COMPARING STRINGS
(CONT.)
Figure 4-17: Two versions of the calcButton_Click procedure
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible28
website, in whole or in part.
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
of them can be selected
at the same time
• Each check box should
be labeled to make its
purpose obvious
Figure 4-18: Sample run of a different version of the
Square Calculations application
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible29
website, in whole or in part.
COMPARING BOOLEAN VALUES
Figure 4-19:
calcButton_Click
procedure
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible30
website, in whole or in part.
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
Figure 4-20: Examples of the English logical operators
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible31
website, in whole or in part.
LOGICAL OPERATORS
(CONT’D.)
Figure 4-21:
How to use
logical
operators in
a condition
(continues)
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible32
website, in whole or in part.
LOGICAL OPERATORS
(CONT’D.)
Figure 4-21: How to
use logical
operators in a
condition
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible33
website, in whole or in part.
LOGICAL OPERATORS
(CONT’D.)
• Truth tables summarize how the computer evaluates
expressions containing a logical operator
• Both sub-conditions combined with the AndAlso operator
need to be True for the compound condition to be True - no
need to evaluate sub-condition2 when subcondition1 is False
• Only one of the sub-conditions combined with the OrElse
operator needs to be True for the compound condition to be
True - no need to evaluate sub-condition2 when subcondition1 is True
• The concept of evaluating sub-condition2 based on the
result of sub-condition1 is referred to as short-circuit
evaluation
Microsoft Visual Basic 2015: Reloaded, Sixth Edition 34
© 2016 Cengage Learning®. May not be scanned,
copied or duplicated, or posted to a publicly accessible
website, in whole or in part.
LOGICAL OPERATORS
(CONT’D.)
Figure 4-22: How to evaluate expressions containing a logical operator
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible35
website, in whole or in part.
LOGICAL OPERATORS
(CONT’D.)
Using the Truth Tables
• Programmers refer to the process of verifying the
input data as data validation
Figure 4-23: Problem specification and partially completed If clauses
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible36
website, in whole or in part.
LOGICAL OPERATORS
(CONT’D.)
Figure 4-24: calcButton_Click event
procedure and sample runs of the application
© 2016 Cengage Learning®. May not be scanned,
copied or duplicated, or posted to a publicly accessible
website, in whole or in part.
SUMMARY OF OPERATORS
Figure 4-25: How to determine the order in which operators are evaluated
© 2016 Cengage Learning®. May not be scanned,
Microsoft Visual
Basic
2015: Reloaded,
Edition
copied
or duplicated,
or posted to Sixth
a publicly
accessible38
website, in whole or in part.
PROGRAMMING TUTORIAL 1
Figure 4-28: TOE chart for the
Find the Robot application
Figure 4-29 MainForm for the
Find the Robot application
© 2016 Cengage Learning®. May not be scanned,
copied or duplicated, or posted to a publicly accessible
website, in whole or in part.
PROGRAMMING TUTORIAL 2
Figure 4-35: TOE chart
for the Bubbles Car
Wash application
Figure 4-36: MainForm for the
Bubbles Car Wash application
© 2016 Cengage Learning®. May not be scanned,
copied or duplicated, or posted to a publicly accessible
website, in whole or in part.
PROGRAMMING EXAMPLE
Figure 4-40: TOE
chart
Figure 4-41: User Interface
© 2016 Cengage Learning®. May not be scanned,
copied or duplicated, or posted to a publicly accessible
website, in whole or in part.
Download