Syntax

Compares one value with a list of CASE values and runs the branch of the first match. CASE DEFAULT runs when nothing matched.

SELECT [CASE] <expression>
  CASE <value> [, <value>…]
    <statements>
  [ENDCASE | END CASE]
  …
  [CASE DEFAULT | DEFAULT CASE
    <statements>]
ENDSELECT
END SELECT

Also written : CASE, CASE DEFAULT, DEFAULT, ENDCASE, ENDSELECT

Parts

<expression>The value to test, read once. A number, a string or any expression.
<value>A value to match: a literal, a variable or an expression such as 2 * 2. Several values separated by commas share one branch. They are tried in order.
<statements>The lines of a branch.

Good to know

  • One branch runs, and only one. There is no fall-through: an empty CASE does nothing and does not run the next one. To share a body, list the values: CASE 2, 3.
  • Matching is strict equality: the string "5" does not match CASE 5. 2.0 matches CASE 2, because it is the same number.
  • ENDCASE is optional: the next CASE closes a branch too. CASE DEFAULT comes once and last: a CASE after it is a compile error.
  • CASE 1 TO 5, CASE IS > 5 and CASE ELSE do not exist. They compile and then stop the program with an error. A bare DEFAULT is not a branch either: its lines end up in the CASE above it. Use an IF … ELSEIF chain for ranges, and CASE DEFAULT for the rest.
  • Do not write a comparison in a CASE: CASE x = 7 stores 7 in x instead of comparing. The SELECT TRUE trick of other dialects does not work.
  • EXIT and CONTINUE inside a SELECT act on the loop around it, not on the SELECT. Outside any loop they are compile errors.

Example

FOR weapon = 1 TO 4  SELECT weapon    CASE 1      Console("sword")   ' sword    CASE 2, 3      Console("bow")     ' bow, bow    CASE DEFAULT      Console("fists")   ' fists  ENDSELECTNEXT
In the manual : Control flow