ELSE (Else)

ELSE Command syntax diagram

 

Purpose

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, the ELSE command can specify a CL command to be processed for the false condition. If the DO command is specified on the ELSE command, a Do group can be processed. If the result of processing the IF command is true, the ELSE command and commands associated with it are not processed.

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.

Restriction: The ELSE command is valid only in a CL program. It must have an associated IF command preceding it.

 

Optional Parameters

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 programs, cannot be specified on the ELSE command:

ENDDO (End Do)

MONMSG (Monitor Message)

PGM (Program)

ENDPGM (End Program)

DCL (Declare CL Variable)

DCLF (Declare File)

another ELSE command

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

Examples for ELSE

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.

Error messages for ELSE

*ESCAPE Messages

CPF2150
Object information function failed.
CPF2151
Operation failed for &2 in &1 type *&3.
CPF8122
&8 damage on library &4.
CPF8123
Damage on object information for library &4.
CPF8129
Program &4 in &9 damaged.
CPF9803
Cannot allocate object &2 in library &3.
CPF9806
Cannot perform function for object &2 in library &3.
CPF9807
One or more libraries in library list deleted.
CPF9808
Cannot allocate one or more libraries on library list.
CPF9810
Library &1 not found.
CPF9811
Program &1 in library &2 not found.
CPF9820
Not authorized to use library &1.
CPF9821
Not authorized to program &1 in library &2.
CPF9830
Cannot assign library &1.
CPF9871
Error occurred while processing.