When (WHEN)

The When (WHEN) command evaluates a logical expression and conditionally processes CL program or ILE CL procedure commands according to the evaluation of the expression. If the logical expression is true (a logical 1), the command (or the group of commands in a Do group) specified in the THEN parameter is processed, and all subsequent When and Otherwise commands in the Select command group are not processed. If the result of the logical expression is false (a logical 0), control passes to the next sequential When or Otherwise command in the Select group.

When an IF, DO, DOWHILE, DOUNTIL, or DOFOR command is specified on the THEN parameter, the entire group of commands is bypassed if the result of the logical expression is false. Control passes to the next When, Otherwise, or End Select command.

When the command or Do group specified by the the THEN parameter is completed, control passes to the next command following the End Select command and processing continues from that command.

Restrictions:

Parameters

Keyword Description Choices Notes
COND Condition Logical value Required, Positional 1
THEN Command Command string Optional, Positional 2

Condition (COND)

Specifies the logical expression that is evaluated to determine a condition in the program and what is done next. Refer to "Logical Expressions" in the CL topic collection in the Programming category in the IBM i Information Center at http://www.ibm.com/systems/i/infocenter/ for a description of logical expressions. Note that variables, constants, and the %SUBSTRING, %SWITCH, and %BINARY built-in functions can be used within the expression.

To test if a pointer has a null value, the CL pointer variable can be compared to the special value *NULL if the CL source is being compiled with the Create CL Module (CRTCLMOD) or Create Bound CL Program (CRTBNDCL) command. If the CL source is being compiled with the Create CL Program (CRTCLPGM) command, compare the CL pointer variable to another CL pointer variable that was declared with ADDRESS(*NULL) and has not been changed.

This is a required parameter.

logical-value
Specify the name of a CL logical variable or a logical expression.

Command (THEN)

Specifies the command or group of commands (in a Do group or If command) that are processed if the result of evaluating the logical expression is true. After the command or Do group is processed, control is passed to the next command after the ENDSELECT command associated with this WHEN command. If the command specified in this parameter is a DO, DOWHILE, DOUNTIL, or DOFOR command, all commands within the Do group are considered to be the command specified by the parameter.

If no command is specified on the THEN parameter (a null THEN), control is passed to the next command after the ENDSELECT command associated with this WHEN command.

If a DO command is specified, only the DO command (not the commands specified within the Do group) is within the parentheses. For example:

WHEN COND(&A *EQ &B)  THEN(DO)
  CMD1
  CMD2
  ...
  ENDDO

If the logical expression evaluates to true and no command is specified on the THEN parameter (a null THEN) control is passed to the next command after the ENDSELECT command associated with this WHEN command.

Any CL command can be specified on the THEN parameter, except the following commands:

Examples

DCL   VAR(&NAME)  TYPE(*CHAR)  LEN(10)
DCL   VAR(&INT)  TYPE(*INT)  LEN(4)
 :
SELECT
  WHEN   COND(&NAME *EQ *CMD)  THEN(DO)
    :   (group of CL commands)
  ENDDO
  WHEN   COND(&INT *EQ 1 & &NAME *EQ *PGM)  THEN(DO)
    :   (group of CL commands)
  ENDDO
ENDSELECT

The WHEN specifies the command to run if its condition is evaluated to true. The WHEN commands in a SELECT group are evaluated in the order they are encountered. If a WHEN condition is not met, processing continues with the next command following the ENDSELECT command.

Error messages

None