ElseIf

ElseIf is a method for doing cascading comparisons.  This is best used when you have a series of conditions, and you want to take an action for the first true condition, or a single action if the are all false.

The syntax for ElseIf is:

If <condition> Then

<code to execute>

ElseIf <condition> Then

<code to execute>

Else

<code to execute>

End If

You may have as many ElseIf's as you want.

Example 1:  You have a simple calculator that adds two numbers.  You want to make sure that the user enter something in both boxes before you do any addition.

If txtNumber1.Text = "" Then

MsgBox "Please Enter Number 1"

ElseIf txtNumber2.Text = "" Then

MsgBox "Please Enter Number 2"

Else

'Code to add the numbers

End If

Example 2:  You want to display the appropriate letter grade, given a number grade.

If (intGrade > 90) Then

lblGrade.Caption = "A"

ElseIf (intGrade > 80) Then

lblGrade.Caption = "B"

ElseIf (intGrade > 70) Then

lblGrade.Caption = "C"

ElseIf (intGrade > 60) Then

lblGrade.Caption = "D"

Else

lblGrade.Caption = "F"

End If

Example 2 clearly shows the cascading effect of a series of ElseIf's.