SELECT command and SELECT groups

The Select (SELECT) command is used to identify one or more conditions and an associated group of commands to process when that condition is true.

A special group of commands may also be specified to be processed when none of the stated conditions are true. Only one of the groups of commands identified by When (WHEN) or Otherwise (OTHERWISE) commands will be processed within the group.

The general structure of the Select (SELECT) command is as follows:

SELECT
  WHEN (condition-1) THEN(command-1)
  . 
  .
  .
  WHEN (condition-n) THEN(command-n)
  OTHERWISE command-x
ENDSELECT

A SELECT group must specify at least one When (WHEN) command. The When (WHEN) command includes an expression, which is tested (true or false), and an optional THEN parameter that specifies the action to take if the condition is true.

The logical expression on the COND parameter may be a single logical variable or constant, or it must describe a relationship between two or more operands; the expression is then evaluate as true or false.

If the condition described by the logical expression is evaluated as true, the procedure processes the CL command on the THEN parameter. This may be a single command or a group of commands specified by the DO, DOWHILE, DOUNTIL, or DOFOR commands. If the condition is not true, the condition specified on the next When (WHEN) command in the SELECT group is evaluated. If there is no When (WHEN) command following this one, the command identified by the Otherwise (OTHERWISE) command, if any, is processed. If there is no next WHEN and no Otherwise (OTHERWISE) command, processing continues with the next command following the associated ENDSELECT command.

SELECT
  WHEN (&LGL)
  WHEN (&INT *LT 0) THEN(CHGVAR &INT 0)
  WHEN (&INT *GT 0) (DOUNTIL (&INT *EQ 0))
                       CHGVAR &INT (&INT - 1)
                     ENDDO
  OTHERWISE (CHGVAR &LGL '1')
ENDSELECT

If the initial value of &LGL is true ('1'), processing continues with the command following the ENDSELECT, because there is no THEN parameter.

If the initial value of &LGL is false ('0'), the COND of the second WHEN is evaluated. If &INT is less than zero, the CHGVAR is processed, setting the value of &INT to zero. Processing then continues with the command following the ENDSELECT.

If the first two conditions are not met, the value of &INT is checked to determine if it is greater than zero. If the value is greater than zero, the DOUNTIL group is entered and the value of &INT decremented until it reaches zero. When &INT reaches zero, the DOUNTIL group is exited and processing continues with the next command following the ENDSELECT.

If none of the conditions on any of the When (WHEN) commands is evaluated as true, the CHGVAR specified on the CMD parameter of the Otherwise (OTHERWISE) command is processed. The value of &INT remains unchanged while &LGL is set to true. Processing then continues with the next command following the ENDSELECT.

Related reference
*AND, *OR, and *NOT operators
Related information
CL command finder
Select (SELECT) command