Nested If..Else Example


Scenario - Displays the maximum number among three numbers.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. NESTEDIE.
       AUTHOR. MTH.
 
       DATA DIVISION.
       WORKING-STORAGE SECTION. 
       01 WS-VAR.
          05 WS-NUM1      PIC 9(03).
          05 WS-NUM2      PIC 9(03).
          05 WS-NUM3      PIC 9(03).

       PROCEDURE DIVISION.

           ACCEPT WS-NUM1
           ACCEPT WS-NUM2
           ACCEPT WS-NUM3.

           IF WS-NUM1 > WS-NUM2
              IF WS-NUM1 > WS-NUM3 
                 DISPLAY "LARGEST NUMBER IS: " WS-NUM1
              ELSE 
                 DISPLAY "LARGEST NUMBER IS: " WS-NUM3
              END-IF
           ELSE 
              IF WS-NUM2 > WS-NUM3
                 DISPLAY "LARGEST NUMBER IS: " WS-NUM2
              ELSE
                 DISPLAY "LARGEST NUMBER IS: " WS-NUM3
              END-IF 
           END-IF. 

           STOP RUN.

JCL -

//MATEPKRJ JOB MSGLEVEL=(1,1),NOTIFY=&SYSUID    
//*
//STEP01  EXEC PGM=NESTEDIE
//STEPLIB  DD  DSN=MATEPK.COBOL.LOADLIB,DISP=SHR
//SYSOUT   DD  SYSOUT=*
//SYSIN    DD  * 
300
450
180
/*

Output -

LARGEST NUMBER IS: 450