COBOL EVALUATE and END-EVALUATE – Quick Reference

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.

Explain Like I'm Five: What Is EVALUATE?

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."

Basic Syntax

cobol
1
2
3
4
5
6
7
8
EVALUATE 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.

WHEN Clause Forms

WHEN clause forms
FormMeaning
WHEN valueMatch when subject equals value (e.g. WHEN "A", WHEN 5).
WHEN value1 THRU value2Match when subject is between value1 and value2 inclusive (numeric or alphabetic range).
WHEN conditionWith EVALUATE TRUE, match when condition is true (e.g. WHEN WS-X > 10).
WHEN cond1 ALSO cond2With EVALUATE TRUE ALSO TRUE, match when both conditions are true.
WHEN OTHERDefault: matches if no previous WHEN matched. Usually last.

Evaluating a Single Variable (Value Match)

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.

cobol
1
2
3
4
5
6
7
8
9
10
11
EVALUATE WS-GRADE WHEN "A" WHEN "B" WHEN "C" DISPLAY "Pass" WHEN "D" WHEN "E" DISPLAY "Fail" WHEN OTHER DISPLAY "Invalid grade" END-EVALUATE.

Range: THRU

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.

cobol
1
2
3
4
5
6
7
8
9
10
EVALUATE 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.

Multiple Conditions: EVALUATE TRUE and ALSO

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.

cobol
1
2
3
4
5
6
7
8
EVALUATE 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

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.

Step-by-Step: Writing an EVALUATE

  1. Choose the subject: a single variable (EVALUATE ws-code) or TRUE for condition-based logic (EVALUATE TRUE ALSO TRUE).
  2. List WHEN clauses in order. Put the most specific or most common cases first if it matters for readability.
  3. For value match: use WHEN value or WHEN v1 WHEN v2 for multiple values with the same action. For ranges use WHEN low THRU high.
  4. For multiple conditions use EVALUATE TRUE ALSO TRUE and in each WHEN write condition ALSO condition. All must be true for that WHEN to run.
  5. Add WHEN OTHER if you need a default (optional). End with END-EVALUATE.

EVALUATE vs Nested IF

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.

Best Practices

Test Your Knowledge

Test Your Knowledge

1. EVALUATE is used for:

  • Opening files
  • Multi-way selection (like a case statement)
  • Defining data
  • Calling programs

2. WHEN 70 THRU 79 means:

  • Values 70 and 79 only
  • Any value from 70 to 79 inclusive
  • Seventy through seventy-nine lines
  • Invalid syntax

3. EVALUATE TRUE ALSO TRUE with WHEN cond1 ALSO cond2 runs the action when:

  • Either condition is true
  • Both conditions are true
  • Only cond1 is true
  • Never