Make your own free website on
2.4.20 The SELECT/WHEN/OTHERWISE Instruction
     SELECT ; whenpart [ whenpart ... ] [ OTHERWISE [;]
          [ statement ... ] ] END ;
          whenpart : WHEN expr [;] THEN [;] statement

This instruction is used for general purpose, nested IF
structures. Although it has certain similarities with CASE in
Pascal and switch in C, it is in some respects very different from
these. An example of the general use of the SELECT instruction is:

          when expr1 then statement1
          when expr2 then do
          when expr3 then statement3

When the SELECT instruction is executed, the next statement after
the SELECT statement must be a WHEN statement. The expression
immediately following the WHEN token is evaluated, and must result
in a valid boolean value. If it is true (i.e. 1), the statement
following the THEN token matching the WHEN is executed, and
afterwards, control is transferred to the instruction following
the END token matching the SELECT instruction. This is not
completely true, since an instruction may transfer control
elsewhere, and thus implicitly terminate the SELECT instruction;
e.g. LEAVE, EXIT, ITERATE, SIGNAL, or RETURN or a condition
trapped by method SIGNAL.

If the expression of the first WHEN is not true (i.e. `0), then
the next statement must be either another WHEN or an OTHERWISE
statement. In the former case, the process explained above is
iterated. In the latter case, the clauses following the OTHERWISE
up to the END statement are interpreted.

It is considered a SYNTAX condition, {7} if no OTHERWISE statement
when none of the WHEN-expressions evaluates to true. In general
this can only be detected during runtime. However, if one of the
WHENs is selected, the absence of an OTHERWISE is not considered
an error.

By the nature of the SELECT instruction, the WHENs are tested in
the sequence they occur in the source. If more than one WHEN have
an expression that evaluates to true, the first one encountered is

If the programmer wants to associate more than one statement with
a WHEN statement, a DO/END pair must be used to enclose the
statements, to make them one statement conceptually. However,
zero, one, or more statements may be put after the OTHERWISE
without having to enclose them in a DO/END pair. The clause
delimiter is optional after OTHERWISE, and before and after THEN.

Example: Writing SWITCH as IF

Although CASE in Pascal and switch in C are in general table-
driven (they check an integer constant and jumps directly to the
correct case, based on the value of the constant), SELECT in REXX
is not so. It is a just a shorthand notation for nested IF
instructions. Thus a SWITCH instruction can always be written as
set of nested IF statements; but for very large SWITCH statements,
the corresponding nested IF structure may be too deeply nested for
the interpreter to handle.

The following code shows how the SWITCH statement shown above can
be written as a nested IF structure:

     if expr1 then statement1
     else if expr2 then do
     end else if expr3 then statement3