Selection Structure
If . . . Then . . Else
Case
Selection Structure
• Use to make a decision or comparison and
then, based on the result of that decision or
comparison, to select one of two paths.
• The condition must result in either a true
(yes) or false (no) answer.
• If the condition is true, the program
performs one set of tasks. If the condition is
false, there may or may not be a different
set of tasks to perform.
Selection Structure Pseudocode
If condition is true Then
perform these tasks
End If
Perform these tasks
whether condition is true
or false
If condition is true then
perform these tasks
Else
perform these tasks
End If
Perform these tasks
whether condition is true
or false
If..Then…Else Statement
If condition Then
[instructions when the condition is true]
[Else
[instructions when the condition is false]]
End If
Relational Operators
=
>
>=
<
<=
<>
• Equal to
• Greater than
• Greater than or equal to
• Less than
• Less than or equal to
• Not equal to
These operators are evaluated from left to right, and are
evaluated after any mathematical operators.
Expressions Containing
Relational Operators
10 + 3 < 5 * 2
• 5 * 2 is evaluated first,
giving 10
• 10 + 3 is evaluated
second, giving 13
• 13 < 10 is evaluated
last, giving false
7 > 3 * 4 / 2
• 3 * 4 is evaluated first,
giving 12
• 12 / 2 is evaluated
second, giving 6
• 7 > 6 is evaluated last,
giving true
All expressions containing a relational operator will result in
either a true or false answer only.
Examples of Relational Operators
used in the condition
• Write a condition that checks if the value stored in
the intNum variable is greater than 123
intNum > 123
• Write a condition that checks if the value stored in
the strName variable is “Mary Smith”
UCase(strName) = “MARY SMITH”
Logical Operators
Not
And
Or
• Reverses the truth value of condition; false
becomes true and true becomes false
• All conditions connected by the And
operator must be true for the compound
condition to be true
• Only one of the conditions connected by
the Or operator needs to be true for the
compound condition to be true.
These operators are evaluated after any mathematical and
relational operators. The order of precedence is Not, And, Or.
Expressions Containing the And
Logical Operator
3 > 2 And 6 > 5
• 3 > 2 is evaluated
first, giving true
• 6 > 5 is evaluated
second, giving
true
• true And true is
evaluated last,
giving true
10 < 25 And 6 > 5 + 1
• 5 + 1 is evaluated first,
giving 6
• 10 < 25 is evaluated
second, giving true
• 6 > 6 is evaluated third,
giving false
• true And false is evaluated
last, giving false
Expression Containing the Or
Logical Operator
8 = 4 * 2 Or 7 < 5
• 4 * 2 is evaluated first, giving 8
• 8 = 8 is evaluated second, giving true
• 7 > 5 is evaluated third, giving false
• true Or false is evaluated last, giving true
All expressions containing a relational operator will result
in either a true or false answer only.
Evaluation of Expressions
Containing Logical Operators
• If you use the And operator to combine two
conditions, Visual Basic does not evaluate
the second condition if the first condition is
false.
• If you use the Or operator to combine two
conditions, Visual Basic does not evaluate
the second condition if the first condition is
true.
Example of Logical Operators
used in the condition
• To pass a course, a student must have an
average test score of at least 75 and an
average project score of at least 35. Write
the condition using the variables sngTest
and sngProj.
sngTest >= 75 And sngProj >= 35
Example of Logical Operators
used in the condition
• Only people living in the state of Michigan
who are over 65 years old receive a
discount. Write the condition using the
variables strState and intAge.
UCase(strState) = “MICHIGAN” And intAge > 65
Example of Logical Operators
used in the condition
• Only employees with job codes of 34 and
67 will receive a raise. Write the condition
using the variable intCode.
intCode = 34 Or intCode = 67
Nested Selection Structure
• A nested selection structure is one in which
either the true path or the false path includes
yet another selection structure.
• Any of the statements within either the true
or false path of one selection structure may
be another selection structure.
Nested If in the true path
If condition1 Then
[instructions when condition1 is true]
If condition2 Then
[instructions when both condition1 and
condition2 are true]
[Else
[instructions when condition1 is true and
condition2 is false]]
End If
Else
[instructions when condition1 is false]]
End If
Nested If in the false path
If condition1 Then
[instructions when condition1 is true]
Else
If condition2 Then
[instructions when condition1 is false and
condition2 is true]
[Else
[instructions when both condition1 and
condition2 are false]]
End If
End If
Nested If Example 1
Write a selection structure that assigns a sales
tax rate to the sngTax variable. The tax rate is
determined by the state code stored in the
intCode variable. Codes of 1 and 3 represent
a 4% rate; a code of 2 represents a 5% rate.
All other codes represent a 2% rate.
Nested If Example 1
If intCode = 1 Or intCode = 3 Then
sngTax = .04
Else
If intCode = 2 Then
sngTax = .05
Else
sngTax = .02
End If
End If
Nested If Example 2
Write a selection structure that assigns a bonus
to the sngBonus variable. The bonus is
determined by the salesperson’s code (intCode)
and, in some cases, by the sales amount
(sngSales). If the code is 1 and the salesperson
sold at least $10,000, then the bonus is $500;
otherwise these salespeople receive $200. If
the code is 2 and the salesperson sold at least
$20,000, then the bonus is $600; otherwise
these salespeople receive $550. All others
receive $150.
Nested If Example 2
If intCode = 1 Then
If sngSales >= 10000 Then
sngBonus = 500
Else
sngBonus = 200
End If
Else
If intCode = 2 Then
If sngSales >= 20000 Then
sngBonus = 600
Else
sngBonus = 550
Else
sngBonus = 150
End If
End If
Nested If Example 2
If intCode = 1 And sngSales >= 10000 Then
sngBonus = 500
Else
If intCode = 1 And sngSales < 10000 Then
sngBonus = 200
Else
If intCode = 2 And sngSales >= 20000 Then
sngBonus = 600
Else
If intCode = 2 And sngSales < 20000
Then
sngBonus = 550
Else
sngBonus = 150
End If
End If
End If
End If
Case Form of the Selection
Structure
• Referred to as the extended selection
structure
• Easier than the nested If to write and
understand
• Typically used when a selection structure
has several paths from which to choose
Select Case Statement
Select Case testexpression
[Case expressionlist1
[instructions for the first Case]]
[Case expressionlist2
[instructions for the second Case]]
[Case expressionlistn
[instructions for the nth Case]]
[Case Else
[instructions for when the
testexpression does not match any of
the expressionlists]]
End Select
To and Is Keywords
• Use the To keyword to specify a range of
values when you know both the minimum
and maximum values
• Use the Is keyword to specify a range of
values when you know only one value,
either the minimum or the maximum
Select Case Example 1
Write a selection structure that assigns a sales
tax rate to the sngTax variable. The tax rate is
determined by the state code stored in the
intCode variable. Codes of 1 and 3 represent
a 4% rate; a code of 2 represents a 5% rate.
All other codes represent a 2% rate.
Select Case Example 1
Select Case intCode
Case 1, 3
sngTax = .04
Case 2
sngTax = .05
Case Else
sngTax = .02
End Select
Select Case Example 2
Write a selection structure that assigns a bonus
to the sngBonus variable. The bonus is
determined by the salesperson’s code (intCode)
and, in some cases, by the sales amount
(sngSales). If the code is 1 and the salesperson
sold at least $10,000, then the bonus is $500;
otherwise these salespeople receive $200. If
the code is 2 and the salesperson sold at least
$20,000, then the bonus is $600; otherwise
these salespeople receive $550. All others
receive $150.
Select Case Example 2
Select Case intCode
Case 1
Select Case sngSales
Case Is >= 10000
sngBonus = 500
Case Else
sngBonus = 200
End Select
Case 2
Select Case sngSales
Case Is >= 20000
sngBonus = 600
Case Else
sngBonus = 550
End Select
Case Else
sngBonus = 150
End Select
Select Case Example 2
Select Case True
Case intCode = 1 And sngSales >= 10000
sngBonus = 500
Case intCode = 1 And sngSales < 10000
sngBonus = 200
Case intCode = 2 And sngSales >= 20000
sngBonus = 600
Case intCode = 2 And sngSales < 20000
sngBonus = 550
Case Else
sngBonus = 150
End Select

ch1. .ppt

  • 1.
    Selection Structure If .. . Then . . Else Case
  • 2.
    Selection Structure • Useto make a decision or comparison and then, based on the result of that decision or comparison, to select one of two paths. • The condition must result in either a true (yes) or false (no) answer. • If the condition is true, the program performs one set of tasks. If the condition is false, there may or may not be a different set of tasks to perform.
  • 3.
    Selection Structure Pseudocode Ifcondition is true Then perform these tasks End If Perform these tasks whether condition is true or false If condition is true then perform these tasks Else perform these tasks End If Perform these tasks whether condition is true or false
  • 4.
    If..Then…Else Statement If conditionThen [instructions when the condition is true] [Else [instructions when the condition is false]] End If
  • 5.
    Relational Operators = > >= < <= <> • Equalto • Greater than • Greater than or equal to • Less than • Less than or equal to • Not equal to These operators are evaluated from left to right, and are evaluated after any mathematical operators.
  • 6.
    Expressions Containing Relational Operators 10+ 3 < 5 * 2 • 5 * 2 is evaluated first, giving 10 • 10 + 3 is evaluated second, giving 13 • 13 < 10 is evaluated last, giving false 7 > 3 * 4 / 2 • 3 * 4 is evaluated first, giving 12 • 12 / 2 is evaluated second, giving 6 • 7 > 6 is evaluated last, giving true All expressions containing a relational operator will result in either a true or false answer only.
  • 7.
    Examples of RelationalOperators used in the condition • Write a condition that checks if the value stored in the intNum variable is greater than 123 intNum > 123 • Write a condition that checks if the value stored in the strName variable is “Mary Smith” UCase(strName) = “MARY SMITH”
  • 8.
    Logical Operators Not And Or • Reversesthe truth value of condition; false becomes true and true becomes false • All conditions connected by the And operator must be true for the compound condition to be true • Only one of the conditions connected by the Or operator needs to be true for the compound condition to be true. These operators are evaluated after any mathematical and relational operators. The order of precedence is Not, And, Or.
  • 9.
    Expressions Containing theAnd Logical Operator 3 > 2 And 6 > 5 • 3 > 2 is evaluated first, giving true • 6 > 5 is evaluated second, giving true • true And true is evaluated last, giving true 10 < 25 And 6 > 5 + 1 • 5 + 1 is evaluated first, giving 6 • 10 < 25 is evaluated second, giving true • 6 > 6 is evaluated third, giving false • true And false is evaluated last, giving false
  • 10.
    Expression Containing theOr Logical Operator 8 = 4 * 2 Or 7 < 5 • 4 * 2 is evaluated first, giving 8 • 8 = 8 is evaluated second, giving true • 7 > 5 is evaluated third, giving false • true Or false is evaluated last, giving true All expressions containing a relational operator will result in either a true or false answer only.
  • 11.
    Evaluation of Expressions ContainingLogical Operators • If you use the And operator to combine two conditions, Visual Basic does not evaluate the second condition if the first condition is false. • If you use the Or operator to combine two conditions, Visual Basic does not evaluate the second condition if the first condition is true.
  • 12.
    Example of LogicalOperators used in the condition • To pass a course, a student must have an average test score of at least 75 and an average project score of at least 35. Write the condition using the variables sngTest and sngProj. sngTest >= 75 And sngProj >= 35
  • 13.
    Example of LogicalOperators used in the condition • Only people living in the state of Michigan who are over 65 years old receive a discount. Write the condition using the variables strState and intAge. UCase(strState) = “MICHIGAN” And intAge > 65
  • 14.
    Example of LogicalOperators used in the condition • Only employees with job codes of 34 and 67 will receive a raise. Write the condition using the variable intCode. intCode = 34 Or intCode = 67
  • 15.
    Nested Selection Structure •A nested selection structure is one in which either the true path or the false path includes yet another selection structure. • Any of the statements within either the true or false path of one selection structure may be another selection structure.
  • 16.
    Nested If inthe true path If condition1 Then [instructions when condition1 is true] If condition2 Then [instructions when both condition1 and condition2 are true] [Else [instructions when condition1 is true and condition2 is false]] End If Else [instructions when condition1 is false]] End If
  • 17.
    Nested If inthe false path If condition1 Then [instructions when condition1 is true] Else If condition2 Then [instructions when condition1 is false and condition2 is true] [Else [instructions when both condition1 and condition2 are false]] End If End If
  • 18.
    Nested If Example1 Write a selection structure that assigns a sales tax rate to the sngTax variable. The tax rate is determined by the state code stored in the intCode variable. Codes of 1 and 3 represent a 4% rate; a code of 2 represents a 5% rate. All other codes represent a 2% rate.
  • 19.
    Nested If Example1 If intCode = 1 Or intCode = 3 Then sngTax = .04 Else If intCode = 2 Then sngTax = .05 Else sngTax = .02 End If End If
  • 20.
    Nested If Example2 Write a selection structure that assigns a bonus to the sngBonus variable. The bonus is determined by the salesperson’s code (intCode) and, in some cases, by the sales amount (sngSales). If the code is 1 and the salesperson sold at least $10,000, then the bonus is $500; otherwise these salespeople receive $200. If the code is 2 and the salesperson sold at least $20,000, then the bonus is $600; otherwise these salespeople receive $550. All others receive $150.
  • 21.
    Nested If Example2 If intCode = 1 Then If sngSales >= 10000 Then sngBonus = 500 Else sngBonus = 200 End If Else If intCode = 2 Then If sngSales >= 20000 Then sngBonus = 600 Else sngBonus = 550 Else sngBonus = 150 End If End If
  • 22.
    Nested If Example2 If intCode = 1 And sngSales >= 10000 Then sngBonus = 500 Else If intCode = 1 And sngSales < 10000 Then sngBonus = 200 Else If intCode = 2 And sngSales >= 20000 Then sngBonus = 600 Else If intCode = 2 And sngSales < 20000 Then sngBonus = 550 Else sngBonus = 150 End If End If End If End If
  • 23.
    Case Form ofthe Selection Structure • Referred to as the extended selection structure • Easier than the nested If to write and understand • Typically used when a selection structure has several paths from which to choose
  • 24.
    Select Case Statement SelectCase testexpression [Case expressionlist1 [instructions for the first Case]] [Case expressionlist2 [instructions for the second Case]] [Case expressionlistn [instructions for the nth Case]] [Case Else [instructions for when the testexpression does not match any of the expressionlists]] End Select
  • 25.
    To and IsKeywords • Use the To keyword to specify a range of values when you know both the minimum and maximum values • Use the Is keyword to specify a range of values when you know only one value, either the minimum or the maximum
  • 26.
    Select Case Example1 Write a selection structure that assigns a sales tax rate to the sngTax variable. The tax rate is determined by the state code stored in the intCode variable. Codes of 1 and 3 represent a 4% rate; a code of 2 represents a 5% rate. All other codes represent a 2% rate.
  • 27.
    Select Case Example1 Select Case intCode Case 1, 3 sngTax = .04 Case 2 sngTax = .05 Case Else sngTax = .02 End Select
  • 28.
    Select Case Example2 Write a selection structure that assigns a bonus to the sngBonus variable. The bonus is determined by the salesperson’s code (intCode) and, in some cases, by the sales amount (sngSales). If the code is 1 and the salesperson sold at least $10,000, then the bonus is $500; otherwise these salespeople receive $200. If the code is 2 and the salesperson sold at least $20,000, then the bonus is $600; otherwise these salespeople receive $550. All others receive $150.
  • 29.
    Select Case Example2 Select Case intCode Case 1 Select Case sngSales Case Is >= 10000 sngBonus = 500 Case Else sngBonus = 200 End Select Case 2 Select Case sngSales Case Is >= 20000 sngBonus = 600 Case Else sngBonus = 550 End Select Case Else sngBonus = 150 End Select
  • 30.
    Select Case Example2 Select Case True Case intCode = 1 And sngSales >= 10000 sngBonus = 500 Case intCode = 1 And sngSales < 10000 sngBonus = 200 Case intCode = 2 And sngSales >= 20000 sngBonus = 600 Case intCode = 2 And sngSales < 20000 sngBonus = 550 Case Else sngBonus = 150 End Select