background image
Original Author Paul Laughton, 2011
Page 87
De Re BASIC!
The value of the argument of Sw.begin is compared to the argument of each Sw.case in order. If any
Sw.case matches the Sw.begin, the statements following the matching Sw.case is executed; if no
Sw.case matches, the statements after Sw.default are executed. Once BASIC! starts to execute the
statements of a Sw.case or Sw.default, it continues execution until it finds a Sw.break or the Sw.end,
ignoring any other Sw.case or Sw.default it may encounter. Sw.break causes a jump to the Sw.end.
In the example:
if the value of a is 1, then <statement1> through <statement2> execute
if the value of a is 2 or 4, then <statement3> through <statement4> execute
if the value of a is less than 0, then <statement5> through <statement6> execute
if a has any other value (3, or more than 4) then <statement7> executes.
A Sw.begin must be followed by a Sw.end, and all of the Sw.case and the Sw.default (if there is one) for
the same switch must appear between them. A set of switch commands is treated as a single unit, just
as if BASIC! were compiled.
Nesting Switch Operations
Switches can be nested. The block of statements following a Sw.case or Sw.default may include a full set
of switch commands. The nested switch begins with another Sw.begin and ends with another Sw.end,
with its own Sw.case and Sw.default statements in between. You can nest other switches inside nested
switches, for as many levels as you want.
If you prefer, you may put the inner switch operations in a labeled Gosub routine or a User-defined
Function, and put the Gosub or function call in the Sw.case or Sw.default block. This may make your
code easier to read, and it will also make the initial scan of the switch a little faster.
Sw.begin <exp>
Begins a switch operation. BASIC! scans forward until it reaches a Sw.end, locating all Sw.case,
Sw.break, and Sw.default statements between the Sw.begin and the Sw.end.
The numeric or string expression <exp> is evaluated. Its value is then compared to the expression(s) in
each Sw.case statement, in order. BASIC! uses to result of the compares to decide which statement to
execute next.
IF this condition is met:
THEN jump to the statement
One or more Sw.Case statement(s) match the Sw.Begin
after the first matching Sw.case.
No Sw.Case matches AND a Sw.default exists
after the Sw.default.
No Sw.Case matches AND no Sw.default exists
after the Sw.end.
There are two forms of Sw.case. You may freely mix both forms. Each form defines what it means to
"match" Sw.begin. Only the first matching Sw.case has any effect.