EVALUATE is COBOL's multi-way selection statement. You specify one or more subjects (e.g. a data item or TRUE), then list WHEN clauses that define matching values or conditions. The first WHEN that matches is executed; optional WHEN OTHER handles all remaining cases. The block is terminated with END-EVALUATE. EVALUATE can replace long IF-ELSE chains and makes range checks (THRU) and multiple conditions (ALSO) easy to write.
Imagine a vending machine: you press a button (A, B, or C) and the machine does one thing for A, another for B, another for C. EVALUATE is like that: the program looks at a value (the subject) and checks it against a list of options (WHEN). As soon as it finds a match, it does that action and stops looking. WHEN OTHER is the "any other button" choice. THRU is like saying "buttons 1 through 5 all give candy;" ALSO is like saying "only when this AND that are true."
12345678EVALUATE subject WHEN value-1 statement(s) WHEN value-2 statement(s) WHEN OTHER statement(s) END-EVALUATE.
The subject is a data item or expression. Each WHEN lists one or more values (or conditions when using EVALUATE TRUE). The first matching WHEN runs its statements; then control goes past END-EVALUATE. WHEN OTHER is optional and matches if no previous WHEN matched.
| Form | Meaning |
|---|---|
| WHEN value | Match when subject equals value (e.g. WHEN "A", WHEN 5). |
| WHEN value1 THRU value2 | Match when subject is between value1 and value2 inclusive (numeric or alphabetic range). |
| WHEN condition | With EVALUATE TRUE, match when condition is true (e.g. WHEN WS-X > 10). |
| WHEN cond1 ALSO cond2 | With EVALUATE TRUE ALSO TRUE, match when both conditions are true. |
| WHEN OTHER | Default: matches if no previous WHEN matched. Usually last. |
When the subject is a variable, each WHEN gives a value (or list of values) to compare. The comparison is equality: WHEN "A" matches when the subject equals "A". You can list multiple values for the same action by writing several WHENs in a row.
1234567891011EVALUATE WS-GRADE WHEN "A" WHEN "B" WHEN "C" DISPLAY "Pass" WHEN "D" WHEN "E" DISPLAY "Fail" WHEN OTHER DISPLAY "Invalid grade" END-EVALUATE.
THRU defines an inclusive range. WHEN 80 THRU 89 matches when the subject is greater than or equal to 80 and less than or equal to 89. Useful for numeric bands (e.g. score ranges) or alphabetic ranges. The type of the subject and the THRU values must be compatible.
12345678910EVALUATE WS-SCORE WHEN 90 THRU 100 MOVE "A" TO WS-LETTER-GRADE WHEN 80 THRU 89 MOVE "B" TO WS-LETTER-GRADE WHEN 70 THRU 79 MOVE "C" TO WS-LETTER-GRADE WHEN OTHER MOVE "F" TO WS-LETTER-GRADE END-EVALUATE.
When you need to test several independent conditions (like multiple IFs combined with AND), use EVALUATE TRUE ALSO TRUE. The subject list is TRUE, TRUE, ... and each WHEN gives conditions connected by ALSO. The first WHEN whose conditions are all true runs. ALSO means "and": both (or all) conditions must be true.
12345678EVALUATE TRUE ALSO TRUE WHEN WS-AGE > 18 ALSO WS-STATUS = "ACTIVE" DISPLAY "Eligible" WHEN WS-AGE <= 18 ALSO WS-STATUS = "ACTIVE" DISPLAY "Minor - check rules" WHEN OTHER DISPLAY "Not active or unknown" END-EVALUATE.
You can use more than two conditions with additional ALSO keywords: EVALUATE TRUE ALSO TRUE ALSO TRUE with WHEN cond1 ALSO cond2 ALSO cond3. Each condition is evaluated; all must be true for that WHEN to match.
END-EVALUATE terminates the EVALUATE block. Every EVALUATE must have a matching END-EVALUATE. After the selected WHEN runs (or when no WHEN matches and there is no WHEN OTHER), control passes to the statement following END-EVALUATE. You can nest EVALUATE statements; each needs its own END-EVALUATE.
Long IF-ELSE-IF-ELSE chains can be clearer as EVALUATE: one subject and a list of WHENs. EVALUATE also makes range checks (THRU) and multiple conditions (ALSO) easy. For one or two simple conditions, IF may be simpler. Choose based on readability and how many cases you have.
1. EVALUATE is used for:
2. WHEN 70 THRU 79 means:
3. EVALUATE TRUE ALSO TRUE with WHEN cond1 ALSO cond2 runs the action when: