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 Statement
If condition is true Then
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
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
Evaluation of Expressions Containing Logical Operators
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.
Example of Logical Operators used in the condition
UCase(strState) = “MICHIGAN” And intAge > 65
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 General form
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 Example
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
Case Form of the Selection Structure
Case Form used when a selection structure has several paths from which to choose
Select Case Structure
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
Select Case Example
Select Case intCode
Case 1
sngTax = .04
Case 2
sngTax = .05
Case Else
sngTax = .02
End Select