Selection Structure
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.
If Statement
End If
If..Then…Else Statement
If condition Then
[Else
End If
Relational Operators
• Write a condition that checks if the value stored in the intNum variable is
greater than 123
• Write a condition that checks if the value stored in the strName variable is
“Mary Smith”
8 = 4 * 2 Or 7 < 5
All expressions containing a relational operator will result in either a true or false answer only.
intCode = 34 Or intCode = 67
• 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.
If condition1 Then
If condition2 Then
[Else
condition2 is false]]
End If
Else
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 used when a selection structure has several paths from which to choose
[Case expressionlist1
[Case expressionlist2
[Case expressionlistn
[Case Else
End Select
Case 1
sngTax = .04
Case 2
sngTax = .05
Case Else
sngTax = .02
End Select