CL Command Reference - ELSE
CL Command List > ELSE Reference
Description:
The Else (ELSE) command is used with an IF command to specify another command that is to be conditionally processed. The ELSE command is processed only if the result of evaluating the logical expression on the preceding IF command is false. If the result is true, the ELSE command and commands associated with it are not processed.
The ELSE command can specify a CL command, or a Do group, to be processed for the false condition.
An ELSE command does not have to follow each IF command, but each ELSE command that is coded must have an associated IF command preceding it. If nested levels of IF commands are used, a given ELSE is always matched with the innermost IF command that has not already been matched with another ELSE command. Although the ELSE command is optional, coding all of the matching ELSE commands makes it easier to see where all of the nesting levels start and end.
Restrictions: The ELSE command is valid only in a CL procedure. It must have an associated IF command preceding it.
Examples:
Example 1: Using ELSE and IF Commands
IF (&A *GT &B) THEN(CHGVAR VAR(&A) VALUE(&B))
ELSE (CHGVAR &B &A)
If the value of &A is greater than the value of &B, &A is set equal to &B. If &A is less than or equal to &B, the test result is false. The CHGVAR command on the ELSE command is processed, and the value of &B is set to the same value as &A. (Refer to the CHGVAR (Change Variable) command for the description of the command and its parameters.)
Example 2: Nested Levels of Commands
IF COND(&A *EQ &B) +
THEN(IF (&C *EQ &D) +
THEN(IF (&C *EQ &F) THEN(DO)))
CMD1
CMD2
:
ENDDO
ELSE CMDX
ELSE CMDY
ELSE DO
This example shows the use of nested levels of IF commands where an ELSE command is associated with each IF. The use of the ELSE commands makes the nested levels of IF commands easier to identify.