Evaluate THRU Example


Scenario - Validating student marks percentage.

Code -

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

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

       PROCEDURE DIVISION.

           EVALUATE WS-MARKS-PERCENT
               WHEN 60 THRU 100
                    DISPLAY "Student got FIRST CLASS" 
               WHEN 50 THRU 59
                    DISPLAY "Student got SECOND CLASS"
               WHEN 35 THRU 49
                    DISPLAY "Student got THIRD CLASS" 
               WHEN OTHER
                    DISPLAY "Student Failed"
           END-EVALUATE.

           STOP RUN.

Output -

Student got FIRST CLASS

Explaining Example -

In the above example: It evaluates the value of the variable WS-MARKS-PERCENT using an EVALUATE statement.

  • If WS-MARKS-PERCENT falls within the range of 60 to 100 (inclusive), it displays "Student got FIRST CLASS".
  • If WS-MARKS-PERCENT falls within the range of 50 to 59 (inclusive), it displays "Student got SECOND CLASS".
  • If WS-MARKS-PERCENT falls within the range of 35 to 49 (inclusive), it displays "Student got THIRD CLASS".
  • If WS-MARKS-PERCENT does not fall within any of the specified ranges, it displays "Student Failed".

It displays "Student got FIRST CLASS" because the WHEN 60 THRU 100 clause is satisfied.