Nested If Example


Scenario - Nested IF..ELSE statement for validating marks percent.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. IFELSEIF.
       AUTHOR. MTH.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-VAR.
          05 WS-MARKS-PERCENT PIC 9(03) VALUE 055.

       PROCEDURE DIVISION.

           IF WS-MARKS-PERCENT > 60
              DISPLAY "GOT FIRST CLASS"
           ELSE
              IF WS-MARKS-PERCENT > 50
                 DISPLAY "GOT SECOND CLASS"
              ELSE
                 IF WS-MARKS-PERCENT > 35
                    DISPLAY "GOT THIRD CLASS"
                 ELSE                        
                    DISPLAY "FAILED"
                 END-IF
              END-IF
           END-IF.

           STOP RUN.

Output -

GOT SECOND CLASS

Explaining Example -

In the above example:

  • If WS-MARKS-PERCENT is greater than 60, it displays "GOT FIRST CLASS".
  • If WS-MARKS-PERCENT is greater than 50 but not more than 60, it displays "GOT SECOND CLASS". So it displays "GOT SECOND CLASS".
  • If WS-MARKS-PERCENT is greater than 35 but not more than 50, it displays "GOT THIRD CLASS".
  • If WS-MARKS-PERCENT is 35 or less, it displays "FAILED".