30
If <boolean-expression> Then
<statements>
ElseIf <boolean-expression> Then
<statements>
Else
<statements>
End If
The ElseIf portion of the statement, including the associated statements, may occur zero or more
times. The Else portion of the statement, along with its associated statements, may occur zero or one
times. Note that the logic of the If-Then-Else statement is designed so that at most one set of statements
gets executed. This construct represents a series of tests that are performed sequentially. The first such
test that produces a Boolean True result will cause the statements associated with that test to be
executed. If none of the tests produce a True result and an Else clause exists, the statements
associated with the Else will be executed. In all cases, after the set of statements is executed, control
transfers to the first statement following the End If.
If the same expression is being repeatedly tested against different values, it is more efficient to use the
Select-Case statement described in Section 2.5.11.
Examples
If (i > 3) Then
Call PutPin(12, zxOutputLow)
Else
j = 55
Call PutPin(12, zxOutputHigh)
End If
If i > 3 Then
Call PutPin(12, zxOutputLow)
ElseIf (i > 0) Then
j = 0
Else
j = 55
Call PutPin(12, zxOutputHigh)
End If
Note that the conditional expression is not required to be enclosed in parentheses. Many programmers
are accustomed to other languages where they are required and therefore do so out of habit. Others
believe that the parentheses improve the readability and use them for that reason. Youre free to adopt
whichever practice suits you.
One other comment on style is in regard to indentation. The examples used in this document indent the
statements within compound statements like If-Then-Else in order to improve readability. The compiler
ignores spaces and tabs except to the extent that they separate identifiers, keywords, etc. Youre free to
adopt any indentation style that you deem appropriate.
There is no fixed limit on how deeply If-Then statements may be nested. The actual limit is governed by
how much memory is available to the compiler. For all practical purposes, there is no limit.
2.5.11 Select-Case Statement
The Select-Case compound statement is a multi-way branch statement that can be used in place of an If-
Then-ElseIf chain is certain situations. The syntax is shown below.