SELECT statement

A select-group provides a multiple path conditional branch. A select-group contains a SELECT statement, optionally one or more WHEN statements, optionally an OTHERWISE statement, and an END statement.

Note: Condition prefixes are invalid on OTHERWISE statements.

Read syntax diagramSkip visual syntax diagramSELECT( exp1);WHEN(,exp2) unit;OTHERWISEunit;

Abbreviation: OTHER for OTHERWISE

SELECT (exp1)
The SELECT statement and its corresponding END statement, delimit a group of statements collectively called a select-group. The expression in the SELECT statement is evaluated and its value is saved.
WHEN (exp2, exp2, …) unit
Specifies one or more expressions that are evaluated and compared with the saved value from the SELECT statement.

If an expression is found that is equal to the saved value, the evaluation of expressions in WHEN statements is terminated, and the unit of the associated WHEN statement is executed. If no such expression is found, the unit of the OTHERWISE statement is executed.

The WHEN statement must not have a label.

OTHERWISE unit
Specifies the unit to be executed when every test of the preceding WHEN statements fails.

If the OTHERWISE statement is omitted and if the execution of the select-group does not result in the selection of a unit, the ERROR condition is raised.

The OTHERWISE statement must not have a label or condition prefix.

unit
Each unit is either a valid single statement, a group, or a begin-block. DECLARE, DEFAULT, END, ENTRY FORMAT, PROCEDURE, and %statement statements are not valid. Each unit can contain statements that specify a transfer of control (for example, GO TO). Hence, the normal sequence of the SELECT statement can be overridden.

If exp1 is omitted, each exp2 is evaluated and converted, if necessary, to a bit string. If any bit in the resulting string is '1'B, the unit of the associated WHEN statement is executed. If all bits are 0 or the string is null, the unit of the OTHERWISE statement is executed.

After execution of a unit of a WHEN or OTHERWISE statement, control passes to the statement following the select-group, unless the normal flow of control is altered within the unit.

If exp1 is specified, each exp2 must be such that the following comparison expression has a scalar bit value:
  (exp1) = (exp2)

Both exp1 and exp2 must be scalar expressions. Hence, while arrays, structures, and unions can be used in either exp1 or exp2, the evaluated expression must be a scalar value.

Examples

In the following example, E, E1, and so on, are expressions. When control reaches the SELECT statement, the expression E is evaluated and its value is saved. The expressions in the WHEN statements are then evaluated in turn (in the order in which they appear), and each value is compared with the value of E.

If a value is found that is equal to the value of E, the action following the corresponding THEN statement is performed; no further WHEN statement expressions are evaluated.

If none of the expressions in the WHEN statements is equal to the expression in the SELECT statement, the action specified after the OTHERWISE statement is executed.
  select (E);
    when (E1,E2,E3) action-1;
    when (E4,E5) action-2;
    otherwise action-n;
  end;
  Nl: next statement;
In the following example, exp1 is omitted:
  select;
    when (A>B) call Bigger;
    when (A=B) call Same;
    otherwise call Smaller;
  end;

If a select-group contains no WHEN statements, the action in the OTHERWISE statement is executed unconditionally. If the OTHERWISE statement is omitted, and if the execution of the select-group does not result in the selection of a WHEN statement, the ERROR condition is raised.