[Next] [Up] [Previous]

The CASE Statement

The CASE statement has the form

       CASE expression;[value,value]:alt,value:alt,...,value:alt 

and causes the alt following a value to be taken if the expression has that value.

Recommended @-comments:

       CASE expression @IS; value @THEN: alt @OR,...

with MATCHING a possible replacement for IS.

For &'...' replacements, the form

        CASE expression &'IS' value &'THEN' alt &'ELSEIS' ...

is available. (&OR and &'OR' do not directly conflict, but it may be desired to RESERVE "or" as both of them if both existed.)

A pair of values separated by a ?3:, or a colon within argument brackets, indicates a range of values. Any number of values may precede an alternative, as multiple values are separated by the equivalent of ?2:`. The illustration of the form of the CASE statement above shows the preferred method of uttering a ?2:`: by enclosing the arguments separated by that separator by argument brackets (`[ and ], or their substitutes ?( and ?)`) within which ; becomes ?1:, , becomes ?2:, : becomes ?3:, and ?n: becomes ?m: where m equals n+3.

The expression MAY be floating-point (REAL, etc.).

An alt at the end of the statement not preceded by a value is taken if no other alternatives are taken.

The following is an example of a CASE statement:

       CASE C;
        ['A':'Z','a':'z','$']: {. M=1},
        ['0':'9','_']:         {. M=2},
                               {. M=0}

and, provided C is a CHARACTER variable, and that the system on which the EXALT program is to be executed orders characters by a sequence similar to ASCII, with the entire alphabet contiguous, this statement will set M to 1 if C contains a character that can begin a EXALT variable name, to 2 if C contains a character that can occur within a variable name in EXALT but not begin it, and to zero otherwise.

A SET (or GROUP) of items of the type of the expression may also appear in place of a value, with membership in the set satisfying the CASE as well as equality to the value. Thus, ['0':'9','_'] could have been replaced by _S('0':'9','_'); the argument brackets also are not needed, as the value of the function _S is a single argument. This is mainly important because values can be expressions as well as constants.

Although the ON statement can be considered to be a special case of this statement, its availability as a separate statement simplifies generating more efficient code for that case, and also allows avoiding typing in the numbers 1, 2, 3, etc. needlessly, thus saving keystrokes.

Values may be expressions; in this case, the statement will likely execute more slowly. If more than one alternative in a CASE statement is satisfied by the initial expression, any of the ones it satisfies (not necessarily the first one) may be taken.


[Next] [Up] [Previous]