//[name] IF [(]relational-expression[)] THEN [comments] . . action when relational-expression is true . //[name] ELSE [comments] . . action when relational-expression is false . //[name] ENDIF [comments]
The below keywords can include a stepname and procstepname to refine the test to a specific job step.
A Keyword List
|ABEND||Tests for an abnormal end of a program|
|ABEND||Tests that an abnormal end of a program did not occur|
|ABENDCC||Examines an ABEND condition code|
|RC||Examines a return code|
|RUN||Tests if a job step executed|
|RUN||Tests if a job step did not execute|
Example 1: Let us see simple IF condition example,
//JOBEXP JOB //STEP01 EXEC MYPROC01 //COND01 IF RC = 0 THEN //STEP02 EXEC MYPROC02 //CONDE ELSE //STEP03 EXEC MYPROC03 // ENDIF
In this example, After STEP01 is completed we check the Return code of STEP01 step. if return code is zero then STEP02 step will execute else step STEP02 will be executed.
IF RC = 0 THEN IF STEP1.RC < 12 THEN
If you not given the stepname, then the highest return code from all job steps is taken for checking.
ABENDCC - Using ABENDCC we can check System/User completion codes.
IF ABENDCC = S0C7 THEN
Suppose you want to check error code of particular step, give stepnname.ABENDCC, If you not given the stepname, most recent ABEND code that occured is taken for checking.
ABEND- checks for an abnormal end of a program
IF ABEND THEN IF STEP4.PROCAS01.ABEND = TRUE THEN
If you not given any stepname, all steps prior to this condition will be checked.
You can nest another IF construct after the THEN clause or the ELSE clause. You can nest IF/THEN/ELSE/ENDIF statement constructs up to 15 levels of nesting.
Either the THEN clause or ELSE clause must contain at least one EXEC statement.
Let's see few more examples below.
//MATEKSD JOB MSGLEVEL=(1,1),NOTIFY=&SYSUID //* //* EXAMPLE TO SHOW IF CONDITION IN JCL //* //STEP01 EXEC PGM=IFCOND1 //* //IFSTMT1 IF STEP01.RC = 0 THEN ---> This will check if the Return Code(RC) //STEP02 EXEC PGM=IFCOND2 from STEP01 is 0, if yes then execute //STEP03 EXEC PGM=IFCOND3 STEP02 and STEP03. // ENDIF //STEP04 EXEC PGM=IFCOND4 //STEP05 EXEC PGM=IFCOND5 //* //IFSTMT2 IF STEP05.RC = 04 THEN ---> This will check if the Return Code(RC) //STEP06 EXEC PGM=IFCOND6 from STEP05 is 04, if yes then execute // ELSE STEP06, else execute STEP07. //STEP07 EXEC PGM=IFCOND7 // ENDIF //STEP08 EXEC PGM=IFCOND8 //*
//JOBIBM JOB CLASS=C,NOTIFY=&SYSUID //* //PRC1 PROC //PST1 EXEC PGM=IDCAMS //PST2 EXEC PGM=SORT // PEND //STEP01 EXEC PGM=SORT //IF1 IF STEP01.RC = 0 THEN //STEP02 EXEC PGM=MYCOBB,PARM=321 // ENDIF //IF2 IF STEP01.RUN THEN //STEP03a EXEC PGM=IDCAMS //STEP03b EXEC PGM=SORT // ENDIF //IF3 IF STEP03b.!ABEND THEN //STEP04 EXEC PGM=MYCOBB,PARM=654 // ELSE // ENDIF //IF4 IF (STEP01.RC = 0 & STEP02.RC <= 4) THEN //STEP05 EXEC PROC=PRC1 // ENDIF //IF5 IF STEP05.PRC1.PST1.ABEND THEN //STEP06 EXEC PGM=IDCAMS // ELSE //STEP07 EXEC PGM=SORT // ENDIF
The return code of STEP01 is tested in IF1. If it is 0, then STEP02 is executed. Else, the processing goes to the next IF statement (IF2).
In IF2, If STEP01 has started execution, then STEP03a and STEP03b are executed.
In IF3, If STEP03b does not ABEND, then STEP04 is executed. In ELSE, there are no statements. It is called a NULL ELSE statement.
In IF4, if STEP01.RC = 0 and STEP02.RC <=4 are TRUE, then STEP05 is executed.
In IF5, if the proc-step PST1 in PROC PRC1 in jobstep STEP05 ABEND, then STEP06 is executed. Else STEP07 is executed.
The IF-THEN-ELSE will not be executed in the case of abnormal termination of the job such as user cancelling the job, job time expiry or a dataset is backward referenced to a step that is bypassed.
If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!