IF command

The IF command is used to state a condition that, if true, specifies some statement or group of statements in the procedure to be run.

The ELSE command can be used with the IF command to specify a statement or group of statements to be run if the condition expressed by the IF command is false.

The command includes an expression, which is tested (true or false), and a THEN parameter that specifies the action to be taken if the expression is true. The IF command is formatted as follows:

IF COND(logical-expression) THEN(CL-command)

The logical expression on the COND parameter may be a single logical variable or constant, or it must describe a relationship between two or more operands; the expression is then evaluated as true or false.

If the condition described by the logical expression is evaluated as true, the procedure processes the CL command on the THEN parameter. This may be a single command, or a group of commands. If the condition is not true, the procedure runs the next sequential command.

Both COND and THEN are keywords on the command, and they can be omitted for positional entry. The following are syntactically correct uses of this command:

IF COND(&RESP=1) THEN(CALL CUS210)
IF (&A *EQ &B) THEN(GOTO LABEL)
IF (&A=&B) GOTO LABEL

Blanks are required between the command name (IF) and the keyword (COND) or value (&A). No blanks are permitted between the keyword, if specified, and the left parenthesis enclosing the value.

The following is an example of conditional processing with an IF command. Processing branches in different ways depending on the evaluation of the logical expression in the IF commands. Assume, for instance, that at the start of the following code, the value of &A is 2 and the value of &C is 4.

       IF (&A=2) THEN(GOTO FINAL)
       IF (&A=3) THEN(CHGVAR &C 5)
       .
       .
       .
FINAL: IF (&C=5) CALL PROGA
       ENDPGM

In this case, the procedure processes the first IF command before branching to FINAL, skipping the intermediate code. It does not return to the second IF command. At FINAL, because the test for &C=5 fails, PROGA is not called. The procedure then processes the next command, ENDPGM, which signals the end of the procedure, and returns control to the calling procedure.

Processing logic would be different if, using the same code, the initial values of the variables were different. For instance, if at the beginning of this code the value of &A is 3 and the value of &C is 4, the first IF statement is evaluated as false. Instead of processing the GOTO FINAL command, the procedure ignores the first IF statement and moves on to the next one. The second IF statement is evaluated as true, and the value of &C is changed to 5. Subsequent statements, not shown here, are also processed consecutively. When processing reaches the last IF statement, the condition &C=5 is evaluated as true, and PROGA is called.

A series of consecutive IF statements are run independently. For instance:

      PGM /*  IFFY  */
      DCL &A..
      DCL &B..
      DCL &C..
      DCL &D..
      DCL &AREA *CHAR LEN(5) VALUE(YESNO)
      DCL &RESP..
      IF (&A=&B) THEN(GOTO END)   /*  IF #1  */
      IF (&C=&D) THEN(CALL PGMA)   /*  IF #2  */
      IF (&RESP=1) THEN(CHGVAR &C 2)  /*  IF #3  */
      IF (%SUBSTRING(&AREA 1 3) *EQ YES) THEN(CALL PGMB)  /*  IF #4  */
      CHGVAR &B &C
      .
      .
      .
END:  ENDPGM

If, in this example, &A is not equal to &B, the next statement is run. If &C is equal to &D, PGMA is called. When PGMA returns, the third IF statement is considered, and so on. An embedded command is a command that is completely enclosed in the parameter of another command.

In the following examples, the Change Variable (CHGVAR) command and the DO command are embedded:

IF (&A *EQ &B) THEN(CHGVAR &A (&A+1))
 
 
IF (&B *EQ &C) THEN(DO)
                     .
                     .
                     .
                     ENDDO
Related reference
*AND, *OR, and *NOT operators
DO command and DO groups
ELSE command
Embedded IF commands
Related information
CL command finder
If (IF) command