FutureBasic Logo

<<    Index    >> FutureBasic 5

select case or select switch   statement



Syntax 1:
select [case] targetExpr
   case testExpr [, testExpr ...] [, max32 testExpr]
[statementBlock]
   [case testExpr [, testExpr ...] [, max32 testExpr]
[statementBlock]...]
   [case else
[statementBlock]]
end select


Syntax 2 (introduced in FB 5.6.1):
select switch integerTargetExpr
   case compileTimeIntegerConstantExpr [, compileTimeIntegerConstantExpr ...] [, max32 compileTimeIntegerConstantExpr]
[statementBlock]
   [case compileTimeIntegerConstantExpr [, compileTimeIntegerConstantExpr ...] [, max32 compileTimeIntegerConstantExpr ]
[statementBlock]...]
   [case else
[statementBlock]]
end select


Syntax 3:
select [case]
   case booleanExpr [, booleanExpr ...] [, max32 booleanExpr]
[statementBlock]
   [case booleanExpr [, booleanExpr ...][, max32 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.

Note 1:
Syntax 2 is a specialized form that works only for integer expressions and integer compile-time constants. It translates to a C switch statement, which is more readable than the C translation of syntax 1. Another advantage is performance: a switch statement can often be compiled to a jump table.

Note 2:
Remember that the 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 3:
select case
   case myVar% = 3
:
   case myVar% = 7
:
   case else
:
end select


See Also:
long if