ZBasic Language Reference
41
ZBasic Microcontrollers
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.
There are two special forms of case expressions that may be used in the <case-expr-list> of a
standard case clause. The first special form is the range expression. This takes the form of two
expressions separated by the keyword To. Both expressions must evaluate to the same type as <test-
expr>. The <test-expr> value will be deemed to select the case clause if the value is greater than or
equal to the value of the expression to the left of the To keyword and less than or equal to the value of the
expression to the right of the To keyword. Effectively, the range expression specifies an inclusive range.
The second special form may be used to implement special test conditions. It has the syntax:
Is <conditional-operator> <expression>
The <conditional-operator> element may be any one of the six conditional operators: =, <>, <, <=,
>, and >=. The <expression> element must be an expression that evaluates to the same type as the
<test-expr>. Note that the construction Is = <expr> yields the same result as simply specifying the
expression value alone.
There is no fixed limit on how deeply Select-Case 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.
Example
Select Case i * j
Case 3
j = 5
Call PutPin(12, zxOutputLow)
Case 4, 5 To 20, 27
j = 1
Call PutPin(13, zxOutputLow)
Case 3, 100, Is > 200, j
j = 0
Case Else
j = -1
End Select
In the example above, if the selection expression evaluates to 3 the statements of the first case clause
will be executed. The fact that the third case clause also has a case value of 3 is of no consequence.
Also note that the case expressions are evaluated every time they are tested. This fact must be kept in
mind for two reasons. Firstly, if the case expression contains a variable whose value changes between
successive executions of the Select-Case statement (a situation that is strongly discouraged), the case
clause that is selected may change even if the selection expression value does not change. Secondly, if
any of the case clause expressions involves a function call, the function may or may not be invoked
depending on the value of the selection expression and the values of the various expressions in the case
clauses preceding it.
|