TSO-ISPF JCL COBOL VSAM DB2 CICS IMS-DB Tools Articles Forum Quiz Interview Q&A

JCL - IF/THEN/ELSE/ENDIF Statements


Another approach to control the job processing is by using IF-THEN-ELSE constructs. This gives more flexibility and user-friendly way of conditional processing.

In JCL, IF condition is better than the COND parameter because of 2 reasons –
  1. IF condition in JCL is easy to code as compared to the COND parameter.

  2. JCL programmers with less experience find COND parameter little confusing and boring and it is easier to read and understand IF-ELSE-END-IF condition in JCL.

Syntax:

//[name] IF  [(]relational-expression[)] THEN   [comments]
    .
    .    action when relational-expression is true
    .
//[name] ELSE   [comments]
    .
    .    action when relational-expression is false
    .
//[name] ENDIF   [comments]

Following is the description of the used terms in the above IF-THEN-ELSE Construct:

name : This is optional and a name can have 1 to 8 alphanumeric characters starting with alphabet, #,$ or @.

Relational-expression : A relational-expression will have a format: KEYWORD OPERATOR VALUE, where KEYWORDS can be RC (Return Code), ABENDCC (System or user completion code), ABEND, RUN (step started execution). An OPERATOR can be logical operator (AND (&), OR (|)) or relational operator (<, <=, >, >=, <>).

comments: The comments field follows THEN, ELSE, and ENDIF after at least one intervening blank.

The below keywords can include a stepname and procstepname to refine the test to a specific job step.

A Keyword List

KeywordPurpose
 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

Syntax:

stepname.procstepname.keyword

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

Explanation:

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 STEP03 will be executed.


Tips:

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.

Example 2:

//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
//*

Example 3:

//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

Explanation:

  • 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.

  • If IF4 evaluates to false, then STEP05 is not executed. In that case, IF5 are not tested and the steps STEP06, STEP07 are not 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!

Are you looking for Job Change? Job Portal