EVALUATE Statement
- EVALUATE validates multiple conditions at a time and controls the program flow based on the first condition match found.
- It is a shorter form for the nested IF...ELSE statements and simplifies the logic when multiple choices are available.
Syntax -
* EVALUATE Condition
EVALUATE condition1 [ALSO condition2]
WHEN value1 [THRU value2]
[ALSO value3]
{statements-block-1}
...
...
...
[WHEN OTHER
{statements-block-2}]
END-EVALUATE.
* EVALUATE True
EVALUATE TRUE
[ALSO TRUE]
WHEN value1 [ALSO value2]
{statements-block-1}
...
...
...
[WHEN OTHER
{statements-block-2}]
END-EVALUATE.
Parameters -
- ALSO - Used to validate multiple conditions with EVALUATE and WHEN.
- THROUGH|THRU - Used to validate rangle of values at WHEN.
- END-EVALUATE - The END-EVALUATE used to end the scope of the EVALUATE statement. END-EVALUATE is not required when EVALUATE statement WHEN phase ended with period.
EVALUATE Working -
Once the EVALUATE statement execution starts, the following things happen -
- The conditions are coded with WHEN; if any condition is satisfied, the statements coded under the WHEN are executed, and control transfers to the following executable statement after EVALUATE.
- If none of the EVALUATE satisfies, the statements coded under WHEN OTHER get executed by default, and control transfers to the next executable statement after EVALUATE.
EVALUATE Types -
EVALUATE can be divided logically into the below types based on their usage in the program -
- Simple EVALUATE
- EVALUATE TRUE
- EVALUATE with THRU
- EVALUATE with grouping multiple WHEN conditions
- EVALUATE...WHEN with combined conditions
Simple EVALUATE -
EVALUATE has only one condition and validates a single variable against multiple values. The statements under the WHEN that match the variable value get executed.
Example -
Scenario - Displaying weekday using day number in week.
WORKING-STORAGE SECTION.
01 WEEK-DAY PIC 9(02).
...
PROCEDURE DIVISION.
...
EVALUATE WEEK-DAY
WHEN 01
DISPLAY "TODAY IS SUNDAY"
...
WHEN 07
DISPLAY "TODAY IS SATURDAY"
WHEN OTHER
DISPLAY "Invalid Input"
END-EVALUATE.
EVALUATE TRUE -
In this format, multiple conditions are evaluated instead of checking a variable's value. EVALUATE has a boolean value (TRUE or FALSE), and WHENs have logical expressions.
Example -
Scenario - Validating weekday using day number in week.
WORKING-STORAGE SECTION.
01 WEEK-DAY PIC 9(02).
...
PROCEDURE DIVISION.
...
EVALUATE TRUE
WHEN WEEK-DAY = 01
DISPLAY "TODAY IS SUNDAY"
...
WHEN WEEK-DAY = 07
DISPLAY "TODAY IS SATURDAY"
WHEN OTHER
DISPLAY "Invalid Input"
END-EVALUATE.
EVALUATE with THROUGH | THRU -
EVALUATE THRU is used to validate the variable with a range of values. The values range should be in ascending order.
Example -
Scenario - Validating student marks.
WORKING-STORAGE SECTION.
01 STD-MARKS PIC 9(03).
...
PROCEDURE DIVISION.
...
EVALUATE STD-MARKS
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.
EVALUATE with grouping multiple WHEN conditions -
EVALUATE with multiple WHEN used to validate the variable with a set of values (different values). Multiple WHENs are grouped and have a set of statements under them.
Example -
Scenario - Validating student grades.
WORKING-STORAGE SECTION.
01 STD-GRADE PIC X(01).
...
PROCEDURE DIVISION.
...
EVALUATE 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.
EVALUATE...WHEN with combined conditions -
EVALUATE with multiple conditions used to validate the set of conditions combined with ALSO. The number of conditions in the EVALUATE should match the number of conditions with the WHEN phrase.
Example -
Scenario - Validating age and gender.
WORKING-STORAGE SECTION.
01 WS-VAR.
05 WS-GENDER PIC X(01).
05 WS-AGE PIC 9(03).
...
PROCEDURE DIVISION.
...
EVALUATE TRUE ALSO TRUE
WHEN WS-AGE > 18 ALSO WS-GENDER = 'M'
DISPLAY 'HE IS MAJOR'
WHEN WS-AGE <= 18 ALSO WS-GENDER = 'M'
DISPLAY 'BOY IS MINOR'
WHEN WS-AGE > 18 ALSO WS-GENDER = 'F'
DISPLAY 'SHE IS MAJOR'
WHEN WS-AGE <= 18 ALSO WS-GENDER = 'M'
DISPLAY 'GIRL IS MINOR'
WHEN OTHER
DISPLAY 'Invalid Input'
END-EVALUATE.