Start Back Next End
  
ZBasic Language Reference
40
ZBasic Microcontrollers
If <boolean-expression> Then <statement> [ Else <statement> ]
The <boolean-expression> element is the same as described in the previous section.  The
<statement> element represents one ZBasic statement such as an assignment statement or a Call
statement.  Note, that it is permissible for <statement> to be multiple statements each separated from
the next by a colon.  Moreover, the line continuation character may be used to distribute the
<statement> over multiple lines.
Examples
If (a > b) Then a = b
If (a > b) Then a = b Else b = 10
If flag Then flag = False : Call MySub(25)
BasicX Compatibility Note
The single-line If statement is not supported in BasicX compatibility mode.
2.5.12 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.
Select Case <test-expr>
Case <case-expr-list>
[<statements>]
...
Case Else
[<statements>]
End Select
The <test-expr> element, known as the selection expression, gives a value that will be tested against
the value(s) given in zero or more standard case clauses.  Each standard case clause begins with the
word Case and is followed by a list of one or more expressions, each of which must evaluate to the same
type as <test-expr>.  If multiple expressions are given, they must be separated from one another by a
comma.  The remainder of the case clause consists of zero or more ZBasic statements.  The type of the
selection expression may be Boolean, an enumeration, any numeric type or String.  The practical
value of using a Boolean type is somewhat limited, however – it’s simpler to just use an If-Then
statement.
There may be at most one default case clause introduced by the keywords Case Else.  The remainder
of the default case clause consists of zero or more ZBasic statements.  If the default case clause is
present, it must be the final case clause.
The Select-Case statement executes by first evaluating the <test-expr>.  Then the resulting value is
compared with the value of each of the expressions in the <case-expr-list> of the first standard case
clause, if present.  The evaluation of the case expressions and the comparison with the test value is done
in order, left to right.  As soon a case expression is found whose value is equal to the test value, the
statements associated with that case clause are executed and then control transfers to the first statement
following the End Select.  If none of the expressions in the first case clause match the <test-expr>
value, the process is repeated with the second standard case clause and so on until all of the standard
case clauses have been tested.  When all of the standard case clauses have been tested without finding
a matching expression value, if a default case clause exists the statements associated with it are
executed.
Previous page Top Next page