JCL IF Statement

JCL - REFERBACK JCL - Procedures

Advertisements


IF statement used to construct the job execution in conditional way.

IF statement used to construct to execute conditionally execute the job steps within the job.

IF/THEN/ELSE/ENDIF statement can have up to 15 levels internally.

IF statement validates the return code of the previous step(s).

The previous step may not be the immediate preceding step of IF condition.

THEN is mandatory along with IF statement.

ELSE with THEN is optional with IF condition.

ENDIF is mandatory along with IF condition.

Every IF condition coded in JCL should end with ENDIF.

IF statement always followed by a relational expression followed by THEN.


Syntax:

 //[name] IF  [(]relational-expression[)] THEN   [comments]
    .
    .    Set of statements for condition true
    .
//[name] [ELSE   [comments]]
    .
    .    Set of statements for condition false
    .
//[name] ENDIF   [comments]

IF statement can be divided into the below parts.


  • name
  • Operation
  • Relational expression
  • Comment

Note:

Do not code comment if the parameter field is blank.


name:


name is optional on IF/THEN, ELSE, and ENDIF statements.

name must be unique within the job step.

name can be ignored/omitted from the second dataset, if all the datasets are used for concatenation.


Rules:

Name always begins in the column 3 and column 1 & 2 contains ‘//’.

name is 1 through 8 characters.

name is a combination of alphanumeric and national characters (@,# and $).

name first character should be alphabetic or national character.

name must be followed by one blank before DD.


Operation:


Operation field describes about the type of operand.

Operation field consists of IF, ELSE, or ENDIF characters.

Operation field can start in any column.

Operation field must be followed by one blank.


Parameters:


Relational expression followed by IF condition with a blank in between.

Relational expression indicates the condition that the system evaluates.

If the expression exceeds single line, continue the expression beginning in column 4 through 16 of the next statement.

Relational-expression consists of:


  • Comparison operators
  • Logical operators
  • NOT (¬) operators
  • Relational-expression keywords
  • Numeric values

Operation field consists of IF, ELSE, or ENDIF characters.


Comment:


Comment used to make a note of current statement.

Comment field follows parameter field.

Comment field should be preceded with a blank after the parameter field.


Example 1:

//IFSTEP2     IF  (ABEND | STEP1.RC > 8) THEN
    or
//IFSTEP2     IF  (ABEND OR STEP1.RC GT 8) THEN

The above example shows the use of the alphabetic characters rather than special characters for comparison operators.


Example 2:

//STEP01     EXEC  PGM=MTHPROG
             .
             .
//IFSTEP     IF  (ABEND | STEP1.RC > 8) THEN
//STEP02     EXEC  PROC=MTHPROC
//IFSTEPE  ENDIF

The IF statement named IFSTEP invokes procedure MTHPROC if either an abend has occurred or returned a return code that is greater than 8 on a previous step of the job.


Example 3:

//STEP1    EXEC  PGM=...
//IFTEST   IF    ¬ABEND THEN
//STEP2    EXEC  PGM=...
//STEP3    EXEC  PGM=...
//         ELSE
//STEP4    EXEC  PGM=...
//         ENDIF

If STEP1 does not abend, STEP2, STEP3 still runs. If STEP1 does abend, STEP4 is the next step to run as prescribed by the ELSE clause.


Example 4:

//JOBIF  JOB RESTART=(STEP3)
//STEP1  EXEC PGM=MTHPROG
//IFTEST IF STEP1.RC=0 | ¬STEP1.RUN THEN
//STEP2  EXEC PGM=...
//STEP3  EXEC PGM=...
//STEP4  EXEC PGM=...
//       ELSE
//STEP5  EXEC PGM=...
//STEP6  EXEC PGM=...
//STEP7  EXEC PGM=...
//       ENDIF

  1. IFTEST will be checked before executing STEP3.
  2. STEP1.RC=0 tests false because STEP1 did not execute and cannot be correctly evaluated.
  3. ¬STEP1.RUN tests true; therefore, STEP3 and STEP4 will execute and STEP5, STEP6, and STEP7 will not execute.

JCL - REFERBACK JCL - Procedures

Advertisements