SELECT (Begin a Select Group)

Free-Form Syntax SELECT {expression}
Code Factor 1 Extended Factor 2
SELECT Blank {expression}

The select group conditionally processes one of several alternative sequences of operations.

There are two forms for the select group.

  1. For the first form, the SELECT statement does not have an operand. The select group consists of:
  2. For the second form, the SELECT statement has an operand. The select group consists of:
    Note:
    • The expression specified as the operand for the SELECT can have any data type. It cannot be a data structure name, an array name, or a figurative constant.
    • The value of the operand for the SELECT statement at the time of the SELECT statement is used for all the comparisons for the WHEN-IN and WHEN-IS statements in the select group. If the value of the expression for the SELECT statement changes during one of the comparisons for the WHEN-IN and WHEN-IS statements, the changed value is not used for subsequent WHEN-IN and WHEN-IS statements. The original value is used for all the WHEN-IN and WHEN-IS comparisons.
Note: In the following discussion, the term "WHEN*" refers to the WHENxx, WHEN, WHEN-IN, and WHEN-IS operations.

After the SELECT statement, control passes to the statement following the first WHEN* condition that is satisfied. All statements are then executed until the next WHEN* statement. Control passes to the ENDSL statement (only one WHEN* block is executed). If no WHEN* condition is satisfied and an OTHER statement is specified, control passes to the statement following the OTHER operations. If no WHEN* condition is satisfied and no OTHER operation is specified, control transfers to the statement following the ENDSL operation of the select group.

Conditioning indicators can be used on the SELECT operation. If they are not satisfied, control passes immediately to the statement following the ENDSL operation of the select group. Conditioning indicators cannot be used on WHEN*, OTHER and ENDSL operations individually.

The select group can be specified anywhere in calculations. It can be nested within IF, DO, or other select groups. The IF and DO groups can be nested within select groups.

If the comparison in more than one WHEN* statement is true, control passes to the statements associated with the first true WHEN* statement.

If a SELECT operation is specified inside a select group, the WHEN* and OTHER operations apply to the new select group until an ENDSL is specified.

For more information, see Structured Programming Operations.

SELECT operation without an operand

  • In the following example:
    1. If X equals 1, do the operations following the first WHEN* statement.
    2. Note: No END operation is needed before the next WHEN operation. Control passes to the ENDSL statement.
    3. If X does NOT equal 1, and if Y=2 and X<10, do the operations following the second WHEN statement.
    4. If neither condition is true, do the operations following the OTHER statement.
    
    SELECT;
    WHEN X = 1;  //  1 
       R = 1; //  1 
       S = 'A';
       //  2 
    WHEN ((Y = 2) AND (X < 10)); //  3 
       R = 2; //  3 
       S = 'B';
    OTHER; //  4 
       R = 3; //  4 
       S = 'C';
    ENDSL;
    
  • The following example shows a select group with conditioning indicators. After the CHAIN operation, if indicator 10 is on, then control passes to the ADD operation. If indicator 10 is off, then the select group is processed.
    *...1....+....2....+....3....+....4....+....5....+....6....+....7...+....
    CL0N01Factor1+++++++Opcode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq....
    C     KEY           CHAIN     FILE                               10
    C  N10              SELECT
    C                   WHEN      X = 1
     *  Sequence 1
    C                   :
    C                   WHEN      Y = 2
     *  Sequence 2
    C                   :
    C                   ENDSL
    C                   ADD       1              N
    

SELECT operation with an operand

  • In the following example:
    1. All the WHEN-IS and WHEN-IN statements perform the comparisons using the value of expression family.child(c).pet(p).age.
    2. If family.child(c).pet(p).age equals one of the values in the %LIST built-in function, do the operations following the WHEN-IN statement.
    3. Note: No END operation is needed before the next WHEN-IS. Control passes to the ENDSL statement.
    4. If family.child(c).pet(p).age equals 1, do the operations following the WHEN-IS statement.
      Note: If family.child(c).pet(p).age has the value 17, the first WHEN-IN comparison is true, so control would not reach this statement.
    5. If family.child(c).pet(p).age is between 10 and 20, as specified by the %RANGE built-in function, do the operations following the WHEN-IN statement.
    6. If none of the conditions is true, do the operations following the OTHER statement.
    
    DCL-DS family QUALIFIED;
       name VARCHAR(25);
       DCL-DS child DIM(10);
          name VARCHAR(25);
          age int(10):
          DCL-DS pets DIM(3);
             name VARCHAR(25);
             age int(10);
          END-DS:
       END-DS;
    END-DS;
    DCL-S c int(10);
    DCL-S p int(10);
    
    SELECT family.child(c).pet(p).age; //  1 
    WHEN-IN %LIST(2 : 5 : 17); /  2 
       R = 1; //  2 
       S = 'a';
       //  3 
    WHEN-IS 1; //  4 
       R = 2; //  4 
       S = 'B';
    WHEN-IN %RANGE(10 : 20); //  5 
       R = 3; //  5 
       S = 'C';
    OTHER; //  6 
       R = 4; //  6 
       S = 'D';
    ENDSL;
    

    For more examples of the SELECT statement with an operand, see WHEN-IN (When the SELECT Operand is IN the WHEN-IN Operand) and WHEN-IS (When the SELECT Operand is Equal to the WHEN-IS Operand).