In this topic, we described about the below sections -
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.
//[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.
- Relational expression
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.
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 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.
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 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.
//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.
//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.
//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.
//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
- IFTEST will be checked before executing STEP3.
- STEP1.RC=0 tests false because STEP1 did not execute and cannot be correctly evaluated.
- ¬STEP1.RUN tests true; therefore, STEP3 and STEP4 will execute and STEP5, STEP6, and STEP7 will not execute.