Else (ELSE)

Where allowed to run:
  • Batch program (*BPGM)
  • Interactive program (*IPGM)
Threadsafe: Yes
Parameters
Examples
Error messages

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.

Top

Parameters

Keyword Description Choices Notes
CMD Command Command string Optional, Positional 1
Top

Command (CMD)

Specifies the command or commands (in a Do group) to be processed if the result of evaluating the expression on the corresponding IF command is false.

If the command specified in this parameter is a DO command, all of the commands specified within the Do group are considered to be part of the command specified by the parameter. If no command is specified, no action is taken for a false condition.

If the command specified by the CMD keyword is not coded on the same line as the keyword, the left parenthesis following CMD must be coded on the same line, followed by a + or - to show continuation. The command and the right parenthesis can then be coded on the next line. For example:

ELSE  CMD(  +
    GOTO C)

If any part of the command continues on the next line, a continuation character (+ or -) must be specified.

If a DO command is specified, only the DO command (not the commands specified as part of the Do group) is placed in parentheses. For example:

ELSE  CMD(DO)
    CMD1
    CMD2
      .
      .
      .
    ENDDO

The following commands, although valid in CL procedures, cannot be specified on the ELSE command:

In addition, the MONMSG command cannot be specified as the next command after the ELSE command.

Top

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.

Top

Error messages

None

Top