Previous Topic Next topic Print topic


SELECT

Purpose

Heads a SELECT-group, which provides a multi-path conditional branch.

Syntax

SELECT[(select-expression)]; 
   WHEN(e⊃1[,e⊃2[,e⊃3[…]]]) action_1; 
   WHEN(e⊃4[,e⊃5[,e⊃6[…]]]) action_2;
   .
   .
   .
   OTHERWISE action_m; 
END;

Abbreviation(s): OTHER for OTHERWISE.

Parameters

select-expression and e⊃1, e⊃2... are all valid expressions, and each action is a single or compound statement, a DO-group, a SELECT-group, an ON-unit, or a BEGIN block.

Description

The SELECT statement heads a SELECT-group, which provides a multi-path conditional branch. The SELECT statement tests the expressions in the SELECT-group and performs a particular action if the result of any test is true.

SELECT;
   WHEN (condition B)
      SELECT;
            WHEN (condition B1) statement_1; 
            WHEN (condition B2) statement_2;
      END;
   WHEN (condition C)
      SELECT;
            WHEN (condition C1) statement_3; 
            WHEN (condition C2) statement_4; 
            OTHERWISE statement_5;
      END;
   OTHERWISE statement_6;
END;

statement_1 is executed when both B and B1 are true conditions and statement_2 is executed when both B and B2 are true conditions. If B is true, but B1 and B2 are false, nothing happens. If condition B is false, the C conditions are checked. statement_5 is executed when condition B is false, condition C is true, and conditions C1 and C2 are false. statement_6 is executed when condition B and condition C are both false.

When control reaches a SELECT statement with a select-expression present, the select-expression is evaluated and its value saved. Next, the expressions in the WHEN clauses are evaluated in the order in which they appear, and each value is compared with the value of select-expression. If a value is found that is equal to the value of select-expression, the action following the corresponding WHEN clause is performed, and no further WHEN clause expressions are evaluated. If none of the expressions in the WHEN clauses is equal to the select-expression, the action specified after the OTHERWISE clause, if present, is executed unconditionally. If there is no OTHERWISE clause and there is no matching WHEN value, the ERROR condition is raised.

Note:

OTHERWISE may be abbreviated to OTHER.

After the action has been performed, control passes to the first executable statement following the SELECT-group, unless the normal flow is changed by the specified action.

If the select-expression is omitted, each WHEN clause expression is evaluated and converted, if necessary, to a bit string. The action after the WHEN clause is performed if any bit in the resulting bit string is a '1'B.

If a SELECT-group does not contain any WHEN clauses, the action in the OTHERWISE clause is executed unconditionally. If the OTHERWISE clause is omitted, and execution of the SELECT-group does not result in the selection of a WHEN clause, the ERROR condition is raised.

In nested SELECT statements, conditions are checked from top to bottom. If two conditions are true, only the first course of action is taken.

You must close each group in nested SELECT-groups with an END statement.

Example

SELECT;
   WHEN (9>B, C,D, FOUND) CALL PROC_1; 
   WHEN (A=B) CALL PROC_2;
   OTHERWISE CALL PROC_3;
END;

In the previous example, select-expression is omitted, so each WHEN clause expression is evaluated and converted to bit strings. The action after the WHEN clause is performed if any bit in any resulting string is a 1.

DECLARE MONTH CHAR(3),
      YEAR PIC'99',
      NO_DAYS FIXED BINARY;
.
.
.
SELECT(MONTH);
   WHEN ('FEB')
      SELECT (MOD(YEAR,4));
            WHEN (0) NO_DAYS = 29;
            OTHERWISE NO_DAYS = 28;
      END;
   WHEN ('APR','JUN','SEPT','NOV') NO_DAYS = 30; 
   OTHERWISE NO_DAYS = 31;
END;

The previous example shows nested SELECT-groups used to set a variable to the number of days in a specified month. Here, the MOD built-in function returns the remainder when YEAR is divided by 4. (The algorithm is incorrect for some century years.)

Restrictions

None.

Previous Topic Next topic Print topic