Simple Evaluate Example


Scenario - Displaying weekday using day number in week.

Code -

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

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-WEEK-DAY     PIC 9(02).

       PROCEDURE DIVISION.

           MOVE 04           TO WS-WEEK-DAY. 

           EVALUATE WS-WEEK-DAY
               WHEN 01
                    DISPLAY "TODAY IS SUNDAY"
               WHEN 02
                    DISPLAY "TODAY IS MONDAY"
               WHEN 03
                    DISPLAY "TODAY IS TUESDAY"
               WHEN 04
                    DISPLAY "TODAY IS WEDNESDAY"
               WHEN 05
                    DISPLAY "TODAY IS THURSDAY"
               WHEN 06
                    DISPLAY "TODAY IS FRIDAY"
               WHEN 07
                    DISPLAY "TODAY IS SATURDAY"
               WHEN OTHER
                    DISPLAY "INVALID INPUT"
           END-EVALUATE.

           STOP RUN.

Output -

TODAY IS WEDNESDAY

Explaining Example -

In the above example:

  • It assigns the value 04 to the variable WS-WEEK-DAY. Then it evaluates the value of WS-WEEK-DAY using an EVALUATE statement.
  • If WS-WEEK-DAY is 01, it displays "TODAY IS SUNDAY".
  • If WS-WEEK-DAY is 02, it displays "TODAY IS MONDAY".
  • If WS-WEEK-DAY is 03, it displays "TODAY IS TUESDAY".
  • If WS-WEEK-DAY is 04, it displays "TODAY IS WEDNESDAY".
  • If WS-WEEK-DAY is 05, it displays "TODAY IS THURSDAY".
  • If WS-WEEK-DAY is 06, it displays "TODAY IS FRIDAY".
  • If WS-WEEK-DAY is 07, it displays "TODAY IS SATURDAY".
  • If WS-WEEK-DAY does not match any of the specified values (01 to 07), it displays "INVALID INPUT".

It displays "TODAY IS WEDNESDAY" because the WHEN 04 clause is satisfied.