Select Case
Syntax 1
SELECT [CASE] targetExpr CASE testExpr [,testExpr ...] [,max32TestExpr ...] [statementBlock] [CASE testExpr [,testExpr ...] [,max32TestExpr ...] [statementBlock]...] [CASE ELSE [statementBlock]] END SELECT
Syntax 2
SELECT [CASE] CASE booleanExpr [,booleanExpr ...] [,max32T booleanExpr ...] [statementBlock] [CASE booleanExpr [,booleanExpr ...][,max32T booleanExpr ...] [statementBlock]...] [CASE ELSE [statementBlock]] END SELECT
Description
The SELECT CASE
statement marks the beginning of a "select block," which must end with an END SELECT
statement. You can use a select block to conditionally execute a block of statements based on a number of tests that you specify. When there is only one test, it's often more convenient to use a LONG IF...[XELSE]...END IF
block. A select block is useful when there are two or more conditions to test.
If you use Syntax 1, targetExpr
must be a numeric or string expression. Each testExpr
has the following syntax:
[=|<>|<|<=|>|>=]expr
where expr
is an expression of the same type as targetExpr
. When the select block executes, targetExpr
is compared against each testExpr
in order. If the testExpr
does not include a comparison operator (<
, >
, etc.), then targetExpr
is compared for equality with expr
; otherwise targetExpr
is compared with expr
using the indicated operator.
If the comparison of targetExpr
with expr
is true, the statementBlock
(if any) following that CASE
statement is executed; then execution continues at the first statement after END SELECT
. If none of the comparisons in any CASE
statement is true, the statementBlock
(if any) following the CASE ELSE
statement is executed; then execution continues at the first statement after END SELECT
. Each statementBlock
can consist of any number of executable statements, possibly including other SELECT...END SELECT
blocks.
If you use Syntax 2, each booleanExpr
must be a numeric expression that can be evaluated as "true" (nonzero) or "false" (zero). Typically, booleanExpr
will be an expression that includes operators such as AND
, OR
, >
, <
, etc. When the select block executes, each booleanExpr
is evaluated in order, until one is found that is nonzero ("true"). Then the statementBlock
(if any) under the corresponding CASE
statement is executed; then execution continues at the first statement after END SELECT
. If every booleanExpr
is zero ("false"), the statementBlock
(if any) following the CASE ELSE
statement is executed; then execution continues at the first statement after END SELECT
.
Notes
Remember that the two syntaxes work differently. It's a common mistake to do something like this:
SELECT CASE myVar% CASE myVar% = 3 : CASE myVar% = 7 : CASE ELSE : END SELECT
Here the programmer probably intended to compare the value of myVar%
to the values 3 and 7; but that's not what this select block does. Instead, it compares the value of myVar%
first to the value of "myVar%=3
" (which is either -1 or 0), and then to the value of "myVar%=7
" (which is either -1 or 0). This block should have been written in one of these ways:
Using Syntax 1:
SELECT CASE myVar% CASE 3 : CASE 7 : CASE ELSE : END SELECT
Using Syntax 2:
SELECT CASE CASE myVar% = 3 : CASE myVar% = 7 : CASE ELSE : END SELECT