IF Statement
The "IF" statement is used to execute a step conditionally based on the condition coded. The IF statement defines a condition with the return code or condition code of the previous steps. If the condition is true, then the step that follows the IF statement gets executed, and if the condition is false, the step execution gets skipped.
IF statements can have up to 15 levels internally, and ENDIF is mandatory for every IF statement.
Syntax -
//[stepname] IF [(relational-expression)] THEN [comments]
.
. Set of statements for condition true
.
//[stepname] [ELSE [comments]]
.
. Set of statements for condition false
.
//[stepname] ENDIF [comments]
Stepname -
Stepname is used to identify the step uniquely in the JCL. Stepname is optional; if coded, stepname should be unique in the entire job.
Rules -
- The stepname should start from the 3rd column.
- Stepname can be 1-8 characters.
- Stepname is a combination of alphanumeric and special characters (@,#, and $).
- One blank/space should follow the stepname before EXEC.
Operation (IF..THEN, ELSE, ENDIF) -
Operation field consists of IF, ELSE, or ENDIF keyword.
Relational Expression (relational-expression) -
A relational expression followed by IF condition specifies the condition that the system evaluates. Relational expression syntax is -
Keyword Operator Value
Keyword |
| ||||||||||||||||||||||
Operators |
|
||||||||||||||||||||||
Value | Abend value to compare |
Comment -
Comment used to make a note of the current statement. The comment field follows the parameter field and should have a blank after the parameter field.
Examples -
Scenario1 - Executing STEP02 when STEP01 abended or RC > 8.
//Jobcard
//STEP01 DD PGM=PROG1
//IFSTEP2 IF ( ABEND | STEP01.RC > 8 ) THEN
//STEP02 EXEC PGM=PROG2
//IFSTEP2E ENDIF
//STEP03 EXEC PGM=PROG3
Scenario2 - Bypassing STEP02 when STEP01 successfully completed (not abended or RC < 8).
//Jobcard
//STEP01 DD PGM=PROG1
//IFSTEP2 IF ¬ABEND THEN
//STEP02 EXEC PGM=PROG2
//IFSTEP2E ENDIF
//STEP03 EXEC PGM=PROG3
Scenario3 - Coding IF..ELSE condition with stepname.
//Jobcard
//STEP01 DD PGM=PROG1
//IFSTEP2 IF ¬ABEND THEN
//STEP02 EXEC PGM=PROG2
//IFELSE2 ELSE
//STEP03 EXEC PGM=PROG3
//IFSTEP2E ENDIF
//STEP04 EXEC PGM=PROG4
Scenario4 - Coding IF..ELSE condition without stepname.
//Jobcard
//STEP01 EXEC PGM=MTHPROG
//IFTEST IF STEP01.RC=0 | ¬STEP01.RUN THEN
//STEP02 EXEC PGM=...
//STEP03 EXEC PGM=...
//STEP04 EXEC PGM=...
// ELSE
//STEP05 EXEC PGM=...
//STEP06 EXEC PGM=...
//STEP07 EXEC PGM=...
// ENDIF
- IFTEST will be checked before executing STEP03.
- STEP01.RC=0 tests false because STEP01 did not execute and cannot be correctly evaluated.
- ¬STEP01.RUN tests true; therefore, STEP03 and STEP04 will execute and STEP05, STEP06, and STEP07 will not execute.