SWITCH <expr>
CASE <constant>{...{<constant>}}
{ CASE <constant>{...{<constant>}} }
<statements>
EXIT
! (any number of case-statement blocks and each statement
! block can have multiple case statement headers)
{ { CASE <constant>{...{<constant>}} }
{ CASE <constant>{...{<constant>}} }
<statements>
EXIT }
{ DEFAULT
<statements>
EXIT }
ENDSWITCH
The <expr> is first evaluated, and then the list of CASE statements is scanned from top to bottom until a match is found between the <expr> and the <constant>. At that point, the <statements> below the matching CASE are executed. If there is no EXIT, the flow of control will continue on through the <statements> following the subsequent CASE statements. (In other words, once a match is found with a CASE, then all subsequent CASE and DEFAULT statements will match, which is why you almost always want to terminate the <statements> associated with a particular CASE with an EXIT.)
The optional DEFAULT statement is like a CASE that always matches, so its sub-statements will be executed if none of the above CASE statements match, or if a previous CASE statement matches but had no EXIT statement, so that control continued to flow.
Multiple CASE statements can share the same set of <statements>, and a single CASE statement can contain a range of values. A range consists of two constants separated by three dots (although one of the two ends of the range can be omitted to indicate an open-ended range). For example:
SWITCH dues
CASE 101...
free'massages = -1
EXIT
CASE 100
CASE 75
free'massages = 3
EXIT
CASE 50...74
free'massages = 1
EXIT
CASE ...49
free'massages = 0
EXIT
ENDSWITCH
In the above SWITCH, if DUES>=101, FREE'MASSAGES is set to -1. If DUES=100 or DUES=75 then FREE'MASSAGES = 3. If DUES>=50 and DUES<=74, the FREE'MASSAGES = 1, and if DUES<=49 then FREE'MASSAGES = 0. If DUES>75 and DUES<100, then none of the CASE statements match, and since no DEFAULT was specified, the SWITCH ends up having no effect.
Note that as is typical with numeric expressions in Basic, they can evaluate to integers or floating point values. Although it is somewhat unorthodox to use a SWITCH with non-integers, there is no law against it.
The <expr> given in the SWITCH statement can be any type, but the <constants> in the CASE statements must match that type. In other words, if the <expr> is a string, then the CASE <constants> must be literal (quoted) strings, as shown here:
SWITCH member'type$
CASE "Platinum"
free'upgrades = 5
free'upgrades = 3
dues = 100
EXIT
CASE "G"..."Gold"
free'upgrades = 3
free'upgrades = 3
dues = 75
EXIT
DEFAULT
free'upgrades = 0
free'upgrades = 1
dues = 50
EXIT
ENDSWITCH
In the above example, the second CASE statement would match when MEMBER'TYPE$>="G" and MEMBER'TYPE$<="Gold" (using normal Basic string comparison logic). As a reminder, A-BASIC treats trailing blanks the same as nulls in string comparisons, so the second CASE would match even if MEMBER'TYPE$="Gold ".
See History below regarding length and compatibility limitations on the use of string constants.
For the more common type of SWITCH (numeric), the CASE constants can also be ASCII constants, or hex/octal constants, for example:
SWITCH ASC(A$[B;1])
CASE 'A'...'Z'
CASE 'a'...'z'
TYP$ = "Alpha"
EXIT
CASE '0'...'9'
TYP$ = "Number"
EXIT
CASE &h80...
TYP$ = "8 bit"
EXIT
CASE ...&o37
TYP$ = "Control"
EXIT
DEFAULT
TYP$ = "Punc"
EXIT
ENDSWITCH
Comments
Note that any statements between the SWITCH statement and the first CASE are actually executed in all cases under A-Shell, whereas they may not be in other implementations of SWITCH statements (such as AMOS). Consequently you should avoid putting any statements there, except perhaps for debugging purposes.
Requires /X:2 or /RC.
History
2018 January, A-Shell 6.5.1624: Eliminate limitation on length of string constants used in CASE statements. Previously the limit was determined by the current STRSIZ.
2006 June, A-Shell 4.8.958: Function added to A-Shell