31
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 its 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.
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)