Elself Clause

Your procedure can include any number of ElseIf statements and conditions. The ElseIf clause always comes before the Else clause. The statements in the ElseIf clause are executed only if the condition in this clause is true.

Let's look at the following procedure fragment:

If myNumber = 0 Then

MsgBox "You entered zero." ElseIf myNumber > 0 Then

MsgBox "You entered a positive number." ElseIf myNumber < 0 Then

MsgBox "You entered a negative number." End if

This example checks the value of the number entered by the user and stored in the variable myNumber. Depending on the number entered, an appropriate message (zero, positive, negative) is displayed. Notice that the Else clause is not used. If the result of the first condition (myNumber = 0) is false, Visual Basic jumps to the next ElseIf statement and evaluates its condition (myNumber > 0). If the value is not greater than zero, Visual Basic skips to the next ElseIf and the condition myNumber < 0 is evaluated.

0 0

Post a comment