ELSE command

The ELSE command is a way of specifying alternative processing if the condition on the associated IF command is false.

The IF command can be used without the ELSE command:

IF (&A=&B) THEN(CALLPRC PROCA)
CALLPRC PROCB

In this case, PROCA is called only if &A=&B, but PROCB is always called.

If you use an ELSE command in this procedure, however, the processing logic changes. In the following example, if &A=&B, PROCA is called, and PROCB is not called. If the expression &A=&B is not true, PROCB is called.

IF (&A=&B) THEN(CALLPRC PROCA)
ELSE CMD(CALLPRC PROCB)
CHGVAR &C 8

The ELSE command must be used when a false evaluation of an IF expression leads to a distinct branch (that is, an exclusive either/or branch).

The real usefulness of the ELSE command is best demonstrated when combined with Do groups. In the following example, the Do group may not be run, depending on the evaluation of the IF expression, but the remaining commands are always processed.


The real usefulness of the ELSE command is best demonstrated when combined with Do groups. In this example, the Do group may not be run, depending on the evaluation of the IF expression, but the remaining commands are always processed.

With the ELSE command you can specify that a command or set of commands be processed only if the expression is not true, thus completing the logical alternatives:


With the ELSE command you can specify that a command or set of commands be processed only if the expression is not true, thus completing the logical alternatives

Each ELSE command must have an associated IF command preceding it. If nested levels of IF commands are present, each ELSE command is matched with the innermost IF command that has not already been matched with another ELSE command.

IF ... THEN ...
IF ...THEN(DO)
         IF ...THEN(DO)
                    .
                    .
                    .
         ENDDO
         ELSE DO
                    IF ...THEN(DO)
                        .
                        .
                        .
                        ENDDO
                    ELSE DO
                        .
                        .
                        .
                        ENDDO
         ENDDO
ELSE IF ... THEN ...
IF ... THEN ...
IF ... THEN ...

In reviewing your procedure for matched ELSE commands, always start with the innermost set.

The ELSE command can be used to test a series of mutually exclusive options. In the following example, after the first successful IF test, the embedded command is processed and the procedure processes the Reclaim Resources (RCLRSC) command:

IF  COND(&OPTION=1) THEN(CALLPRC PRC(ADDREC))
 ELSE    CMD(IF COND(&OPTION=2) THEN(CALLPRC PRC(DSPFILE)))
  ELSE   CMD(IF COND(&OPTION=3) THEN(CALLPRC PRC(PRINTFILE)))
    ELSE CMD(IF COND(&OPTION=4) THEN(CALLPRC PRC(DUMP)))
RCLRSC
RETURN
Related reference
IF command
*AND, *OR, and *NOT operators
Related information
CL command finder
Else (ELSE) command