CL Command Reference - IF

CL Command List > IF Reference

Description:

The If (IF) command evaluates a logical expression and conditionally processes 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 the ELSE command with its associated command or Do group is not processed. If the result of the logical expression is false (a logical 0), the command specified in the THEN parameter is not processed and control passes to the next command. If that command is an ELSE command, the command or Do group specified in that command is processed. If the ELSE command is not specified, control passes to the next command.

When a DO command is specified, either in the THEN parameter of the IF command or in the CMD parameter of the ELSE command, the Do group is bypassed if the result of the expression is not the one needed for the group being processed. That is, control passes to the command that follows the ENDDO command associated with that DO.

When the command or Do group specified by the THEN parameter or the ELSE command is completed (and no GOTO command has been processed), control passes to the next command following the command or Do group specified by the ELSE command. If a GOTO command is processed, control is passed to the command with the label specified by the GOTO command, and processing continues from that command.

Restrictions:
. The IF command is valid only in CL procedures.
. Up to ten levels of nested IF and ELSE commands are allowed.


Examples:

IF COND(&A *EQ &B) THEN(GOTO X)
IF (&A *EQ &B) THEN(GOTO X)
IF (&A *EQ &B) (GOTO X)
IF COND(&A *EQ &B) THEN(GOTO X)

The examples above show a number of different ways the IF command can be specified to test a condition and branch to a label. In each of these examples, if &A equals &B, control passes to a CL command that has a label named X.

IF COND(&TESTSW) THEN(CHGVAR VAR(&A) VALUE(23))

If &TESTSW has a logical value of 1 (true), the CHGVAR command is processed to set &A to decimal 23; if &TESTW has a logical value of 0 (not true), the Change Variable (CHGVAR) command is not processed.

IF COND((&ALPHA *EQ &BETA) *AND *NOT &GAMMA)
THEN(RETURN)

If the value of &ALPHA equals the value of &BETA and if &GAMMA is a logical 0, then return to the program or procedure that called this CL procedure.

IF &LOG1 THEN(IF (&A *GT 10) THEN(GOTO X))
ELSE(GOTO Y)
ELSE DO
: (group of CL commands)
ENDDO

This is an example of nested IF commands. If &LOG1 has a logical value of 1 (true) and if &A is greater than 10, a branch is made to label X. If &LOG1 has a logical value of 1 and &A is not greater than 10, a branch is made to label Y. If &LOG1 has a logical value of 0 (false), &A is not compared to 10. Instead, the DO group of the second ELSE command is processed.