SELECTION CONSTRUCT

advertisement
SELECTION CONSTRUCT
If...Then...Else
There are four different variations of the If...Then...Else construct. They are listed here:
If...Then...Else
If...Then...Elseif
Nested If on the Then Condition
Nested If on the Else Condition
Each of the If variations are explained below:
1) If... Then...Else
The If... Then...Else allows conditional execution, based on the evaluation of an expression.
The expression could be mathematical, based on strings or there could be more that one
expression that will allow the conditional execution.
The simplest and most powerful form of the if..then ..else statement is:
Syntax:
If <condition1> then
<instructions for condition1 are executed only if condition 1 is true>
Else
<a different set of instructions are executed if condition1 is false>
End If
There are other forms of selection are used when the programmer tests to determine if
more than one condition is true.
Example #1
Dim Large as Integer, Small as Integer, Y as Integer
Large = 20
Y = 21
If Large < Y then
Large = Y
Else
Small = Y
End If
2) If..Then...Else..If
Syntax:
If <condition1> Then
<instructions when condition1 one is true>
ElseIf <condition2> Then
<instructions when condition2 is true>
Else
<instructions to be executed when the conditions are both false>
End If
Example #2
Private Sub GreaterNums_Click ()
Dim X, Y ' Declare variables.
txtYNumber.Text = Y
X = InputBox("Enter a number greater than 0 and less than 1000.")
If X < 10 Then
Y = 1 ' X is 1 digit long.
ElseIf X < 100 Then
Y = 2 ' X is 2 digits long.
Else
Y = 3 ' X is 3 digits long.
End If
End Sub
3) Nested if on the Then Clause
Syntax:
If <condition1> Then
<instructions when condition1 one is true>
If <condition2> Then
<instructions when condition1 and condition2 are true>
Else
<instructions to be executed when condition1 is true and condition2 is false>
End If
Else
<Instructions when condition1 is false>
End if
***NOTE: The If statements MUST BE indented for readability.
Example #3
Bonus = 1000.00
If CurrentMonthSales > LastMonthSales Then
' 3% Commission due to a great month
Commission = CurrentMonthSales *.03
If CurrenMonthSales > QuarterSales Then
' 3% Commission plus bonus.
Commission = CurrentMonthSales *.03 +Bonus
Else
' 3% Commission and no bonus because sales did not exceed
' QuarterSales.
Commission = CurrentMonthSales *.03
End if
Else
' Current month sales were not greater than last month sales therefore
' commission is only 1%.
Commission = CurrentMonthSales *.01
End If
4) Nested if on the Else Clause
Syntax:
If <condition1> Then
<instructions when condition1 one is true>
Else
If <condition2> Then
<instructions when condition1 is false and condition2 is true>
Else
<instructions to be executed when condition1 and condition2 are both false>
End If
End if
Example #4
If <Team1Points > Team2Points> then
MsgBox "Team 1 has won the game"
Else
If <Team2Points > LastGamePoints2> then
MsgBox "Team 1 has lost the game. Team 2 has won and has scored better than their last game"
Else
MsgBox"Team 1 has lost. Team 2 has won the game and has not scored better than the last
game"
End If
End If
GO TO ACTIVITIES 3.1.1 TO 3.1.4 FOR IF..ELSE
PRACTICE
Download