Evaluate True Example


Scenario - Validating weekday using day number in week.

Code -

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

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

       PROCEDURE DIVISION.

           EVALUATE TRUE
               WHEN WS-WEEK-DAY = 1
                    DISPLAY "TODAY IS SUNDAY" 
               WHEN WS-WEEK-DAY = 2
                    DISPLAY "TODAY IS MONDAY" 
               WHEN WS-WEEK-DAY = 3
                    DISPLAY "TODAY IS TUESDAY"
               WHEN WS-WEEK-DAY = 4
                    DISPLAY "TODAY IS WEDNESDAY"
               WHEN WS-WEEK-DAY = 5
                    DISPLAY "TODAY IS THURSDAY" 
               WHEN WS-WEEK-DAY = 6
                    DISPLAY "TODAY IS FRIDAY"
               WHEN WS-WEEK-DAY = 7
                    DISPLAY "TODAY IS SATURDAY"
               WHEN OTHER
                    DISPLAY "INVALID INPUT"
           END-EVALUATE.

           STOP RUN.

Output -

INVALID INPUT

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 equal to 1, it displays "TODAY IS SUNDAY".
  • If WS-WEEK-DAY is equal to 2, it displays "TODAY IS MONDAY".
  • If WS-WEEK-DAY is equal to 3, it displays "TODAY IS TUESDAY".
  • If WS-WEEK-DAY is equal to 4, it displays "TODAY IS WEDNESDAY".
  • If WS-WEEK-DAY is equal to 5, it displays "TODAY IS THURSDAY".
  • If WS-WEEK-DAY is equal to 6, it displays "TODAY IS FRIDAY".
  • If WS-WEEK-DAY is equal to 7, it displays "TODAY IS SATURDAY".
  • If WS-WEEK-DAY does not match any of the specified values (1 to 7), it displays "INVALID INPUT".

It displays "INVALID INPUT" because the WHEN OTHER clause is satisfied.