COBOLbeginner

EVALUATE Statement

Use EVALUATE as COBOL's structured multi-branch decision construct, equivalent to switch/case but far more flexible.

EVALUATE replaces nested IF chains. Legacy COBOL is full of the nesting, so this is the standard "clean this up" question.

EVALUATE is a restaurant host checking a series of yes/no questions in order — party size, reservation, walk-in — and seating you at the first matching table type instead of asking you fifteen separate nested questions.

Key Concepts

1
The common form tests conditions, not a value.
    EVALUATE TRUE
        WHEN WS-AGE < 18            MOVE 'MINOR'  TO WS-CATEGORY
        WHEN WS-AGE < 65            MOVE 'ADULT'  TO WS-CATEGORY
        WHEN OTHER                  MOVE 'SENIOR' TO WS-CATEGORY
    END-EVALUATE
2
EVALUATE TRUE means "find the first condition that is true". Each WHEN can test something completely different — different variables, different comparisons — which is more than a switch in most languages allows.
EVALUATE TRUEWHENswitch
3
It can also test a value directly.
    EVALUATE WS-STATUS-CODE
        WHEN 'A'       ...
        WHEN 'C' THRU 'F'  ...
        WHEN OTHER     ...
    END-EVALUATE
4
ALSO tests combinations, which is where it earns its place:
ALSO
5
    EVALUATE WS-TYPE ALSO WS-STATUS
        WHEN 'PREMIUM' ALSO 'ACTIVE'   PERFORM FULL-SERVICE
        WHEN 'PREMIUM' ALSO ANY        PERFORM LIMITED-SERVICE
        WHEN ANY       ALSO 'CLOSED'   PERFORM REJECT
        WHEN OTHER                     PERFORM STANDARD
    END-EVALUATE
6
That is a decision table. As nested IFs it would be three levels deep and hard to check for completeness; here the combinations are in a column you can read down.
IF
7
ANY means "don't care in this position".
ANY
8
There is no fallthrough. Unlike C, the first matching WHEN runs and control leaves the statement. No break is needed and none exists.
WHENbreak
9
Order matters, because first match wins. In the age example, WHEN WS-AGE < 65 never sees anyone under 18, because the previous branch already caught them. Reorder those two and the logic is wrong while still compiling and running.
WHEN WS-AGE < 65
10
Always include WHEN OTHER. Without it, an unexpected value falls through the whole statement and nothing happens — no assignment, no error, and a field left holding whatever it had. A WHEN OTHER that logs the unexpected value turns a silent wrong answer into a visible one.
WHEN OTHER
11
What the interviewer is probing.1. "What does EVALUATE TRUE give you?" Probing: the idiom. Stalls: "A switch." Moves up: a clean multi-branch on arbitrary conditions, replacing deeply nested IF chains that nobody can read.
12
2. "Does control fall through to the next WHEN?" Probing: the semantics. Stalls: "Yes." Moves up: no — the first matching branch runs and control passes to END-EVALUATE, unlike C-style switch.
13
3. "What is WHEN OTHER for?" Probing: the discipline. Stalls: "Nothing." Moves up: the catch-all — omitting it means an unexpected value silently does nothing, which is how bad data passes through unnoticed.
14
4. "What does an EVALUATE with two subjects do?" Probing: the depth. Stalls: "Nothing." Moves up: it compares pairs of values, giving a decision-table structure that is far clearer than nested conditions.