SWITCH

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

 

Compatibility: requires /X:2 or /RC. Implementation is believed to be compatible with BASIC Plus at the source level, but that implementation was never popular, and the details of its implementation were a bit vague; consequently there has been little effort to determine whether the A-Shell and AMOS implementations are identical or not.

The compiler supports the SWITCH / ENDSWITCH / CASE / DEFAULT statements in /RC and /X:# modes. In either case the generated RUN file is compatible with existing A-Shell runtime versions; in the case of /RC mode, it is also compatible with the AMOS runtime.

The implementation of SWITCH and related statements is roughly compatible with the AMOS BASICPlus version. Since the details of the AMOS version are a bit vague, however, an alternate explanation is provided below.

The general form of the SWITCH construct is as follows:

SWITCH <expr>

     CASE <constant>

          <statements>

          EXIT

     CASE <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'MASSAGES = 3

        DUES = 100

        EXIT

    CASE "G"..."Gold"

        FREE'UPGRADES = 3

        FREE'MASSAGES = 3

        DUES = 75

        EXIT

    DEFAULT

        FREE'UPGRADES = 0

        FREE'MASSAGES = 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, AlphaBASIC treats trailing blanks the same as nulls in string comparisons, so the second CASE would match even if MEMBER'TYPE$="Gold   ".

Note that the maximum length of the literal strings in the CASE statements must not exceed the current default string size as set by the STRSIZ statement. Also note that this form of SWITCH (i.e. with string expressions) does not appear to be supported by AMOS.

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

 

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.

History

2006 June, A-Shell 4.8.958:  Function added to A-Shell