ELSE Statement

Purpose

The ELSE statement provides an alternative execution path in JCL conditional processing. It follows a THEN block and specifies the JCL statements to be executed when the preceding IF condition evaluates to false.

Syntax

jcl
1
2
3
4
5
//[name] IF (condition) THEN // statements to be executed if condition is true //[name] ELSE // statements to be executed if condition is false //[name] ENDIF

Key Points

  • ELSE is optional in IF/THEN/ENDIF structures
  • It appears after the THEN block and before the ENDIF statement
  • It introduces statements to be executed when the condition is false
  • Only one ELSE is allowed per IF statement

Examples

Basic IF/THEN/ELSE Structure

jcl
1
2
3
4
5
// IF STEP1.RC = 0 THEN //STEP2 EXEC PGM=NORMALPROC // ELSE //STEP2 EXEC PGM=ERRORPROC // ENDIF

Conditional SET with ELSE

jcl
1
2
3
4
5
6
7
8
9
10
11
// SET ENV='TEST' // IF &ENV = 'PROD' THEN // SET DATASET='PROD.MASTER' // SET SYSOUT='A' // ELSE // SET DATASET='TEST.MASTER' // SET SYSOUT='X' // ENDIF //STEP1 EXEC PGM=REPORT //INPUT DD DSN=&DATASET,DISP=SHR //OUTPUT DD SYSOUT=&SYSOUT

Nested IF/THEN/ELSE

jcl
1
2
3
4
5
6
7
8
9
10
// IF &SYSDAY = 'FRIDAY' THEN // IF &SYSTIME > '17:00' THEN // SET BACKUP='FULL' // ELSE // SET BACKUP='INCR' // ENDIF // ELSE // SET BACKUP='QUICK' // ENDIF //STEP1 EXEC PGM=BACKUP,PARM='&BACKUP'

Notes

  • The ELSE statement is optional in IF/THEN/ENDIF structures
  • If there is no ELSE and the condition is false, execution continues at the ENDIF statement
  • The name field in the ELSE statement is optional and is typically omitted
  • ELSE applies to the most recent IF that hasn't been matched with an ELSE
  • In nested IF statements, each ELSE is paired with the closest preceding unmatched IF
  • JCL statements in the ELSE block can include any valid JCL, including EXEC, DD, and SET statements

Common Mistakes

  • Adding multiple ELSE statements for a single IF
  • Incorrectly pairing ELSE with nested IF statements
  • Forgetting the ENDIF statement after the ELSE block
  • Using ELSE without a preceding IF/THEN

Related Concepts

Related Pages