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 SELECTAlso 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
CASEdoes 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 matchCASE 5.2.0matchesCASE 2, because it is the same number. ENDCASEis optional: the nextCASEcloses a branch too.CASE DEFAULTcomes once and last: aCASEafter it is a compile error.CASE 1 TO 5,CASE IS > 5andCASE ELSEdo not exist. They compile and then stop the program with an error. A bareDEFAULTis not a branch either: its lines end up in the CASE above it. Use anIF … ELSEIFchain for ranges, andCASE DEFAULTfor the rest.- Do not write a comparison in a CASE:
CASE x = 7stores 7 inxinstead of comparing. TheSELECT TRUEtrick of other dialects does not work. EXITandCONTINUEinside 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 ENDSELECTNEXTIn the manual : Control flow