Start Back Next End
  
ZBasic Language Reference
39
ZBasic Microcontrollers
Although the construction described above is useful, it is often the case that you want your program to
execute a certain set of statements if a condition is true but you want it to execute a different set of
statements if the condition is false.  The If statement allows this logic using the syntax shown below.
If <boolean-expression> Then
    <statements>
Else
    <statements>
End If
Sometimes you’ll want to test several different conditions and execute a different set of statements in
each case.  The If statement allows this logic using the following syntax:
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 0.
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.  You’re 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.  You’re free to
adopt any indentation style that you deem appropriate.
Previous page Top Next page