40 of 172
ZBasic Language Reference 33 ZX Microcontroller Family 2.5.10 If-Then-Else Statement The if-then-else compound statement is the basic decision making construct in ZBasic. In its simplest form, the syntax is: If <boolean-expression> Then <statements> End If The <boolean-expression> element is an expression whose value is of type Boolean. It most often involves one of the conditional operators that allow you to compare the values of two expressions but it may also simply be the invocation of a function whose return type is Boolean. The <statements> element represents zero or more ZBasic statements possibly including other If statements. This allows you to create nested decision-making statements of arbitrary complexity. 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 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