Evaluate using Multiple When Conditions Example


Scenario - Validating student grades.

Code -

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

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-STD-GRADE    PIC X(01) VALUE "E".

       PROCEDURE DIVISION.

           EVALUATE WS-STD-GRADE
               WHEN "A"
               WHEN "B"
               WHEN "C"
               WHEN "D"
                    DISPLAY "Student got FIRST CLASS" 
               WHEN "E"
                    DISPLAY "Student got SECOND CLASS"
               WHEN "F"
               WHEN "G"
                    DISPLAY "Student got THIRD CLASS"
               WHEN OTHER
                    DISPLAY "Student Failed"
           END-EVALUATE.

           STOP RUN.

Output -

Student got SECOND CLASS

Explaining Example -

In the above example: it evaluates the value of the variable WS-STD-GRADE using an EVALUATE statement.

  • If WS-STD-GRADE is "A", "B", "C", or "D", it displays "Student got FIRST CLASS".
  • If WS-STD-GRADE is "E" or "G", it displays "Student got SECOND CLASS".
  • If WS-STD-GRADE is "F", it displays "Student got THIRD CLASS".
  • If WS-STD-GRADE does not match any of the specified values, it displays "Student Failed".

It displays "Student got SECOND CLASS" because the WHEN "E" clause is satisfied.