COBOL EVALUATE
EVALUATE is COBOL's multi-way decision, similar to switch/match in other languages but more powerful: it can
test values, ranges, conditions and even several subjects at once, like a decision table.
EVALUATE subject
WHEN value-1
statements
WHEN value-2 THRU value-3
statements
WHEN OTHER
statements
END-EVALUATEThe WHEN branches are checked from top to bottom, and only the first match runs. There is no fall-through
and no break to forget.
Evaluating a value
IDENTIFICATION DIVISION.
PROGRAM-ID. EVAL-VALUE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-MONTH PIC 99 VALUE 4.
01 WS-SEASON PIC X(6).
PROCEDURE DIVISION.
EVALUATE WS-MONTH
WHEN 12 WHEN 1 WHEN 2
MOVE "Winter" TO WS-SEASON
WHEN 3 THRU 5
MOVE "Spring" TO WS-SEASON
WHEN 6 THRU 8
MOVE "Summer" TO WS-SEASON
WHEN 9 THRU 11
MOVE "Autumn" TO WS-SEASON
WHEN OTHER
MOVE "??????" TO WS-SEASON
END-EVALUATE
DISPLAY "Month " WS-MONTH " is in " WS-SEASON
STOP RUN.Month 04 is in Spring
Several WHEN lines in a row share one set of statements (WHEN 12 WHEN 1 WHEN 2).
EVALUATE TRUE
EVALUATE TRUE checks each WHEN condition in turn and runs the first one that is true. It replaces long
IF … ELSE IF … chains:
IDENTIFICATION DIVISION.
PROGRAM-ID. EVAL-TRUE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-INCOME PIC 9(6) VALUE 48000.
01 WS-RATE PIC 99.
PROCEDURE DIVISION.
EVALUATE TRUE
WHEN WS-INCOME < 12000
MOVE 0 TO WS-RATE
WHEN WS-INCOME < 40000
MOVE 20 TO WS-RATE
WHEN WS-INCOME < 100000
MOVE 35 TO WS-RATE
WHEN OTHER
MOVE 45 TO WS-RATE
END-EVALUATE
DISPLAY "Income " WS-INCOME " -> tax band " WS-RATE "%"
STOP RUN.Income 048000 -> tax band 35%
The order matters: 48000 is not below 12000 or 40000, so the first two tests fail and the third branch wins. Each
WHEN can be this short only because the earlier branches have already removed the smaller incomes. Put the most
specific conditions first.
EVALUATE TRUE works beautifully with level-88 condition names:
IDENTIFICATION DIVISION.
PROGRAM-ID. EVAL-88.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TXN-TYPE PIC X VALUE "W".
88 TXN-DEPOSIT VALUE "D".
88 TXN-WITHDRAWAL VALUE "W".
88 TXN-TRANSFER VALUE "T".
PROCEDURE DIVISION.
EVALUATE TRUE
WHEN TXN-DEPOSIT DISPLAY "Credit the account"
WHEN TXN-WITHDRAWAL DISPLAY "Debit the account"
WHEN TXN-TRANSFER DISPLAY "Move between accounts"
WHEN OTHER DISPLAY "Unknown transaction"
END-EVALUATE
STOP RUN.Debit the account
Several subjects: decision tables
EVALUATE can test several subjects joined by ALSO. Each WHEN then gives one object per subject, and ANY
matches anything. This turns a business decision table directly into code:
| Customer type | Order total | Discount |
|---|---|---|
| Gold | any | 15% |
| Silver | ≥ 500 | 10% |
| Silver | < 500 | 5% |
| other | ≥ 1000 | 5% |
| other | < 1000 | 0% |
IDENTIFICATION DIVISION.
PROGRAM-ID. EVAL-ALSO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TYPE PIC X(6).
01 WS-TOTAL PIC 9(5).
01 WS-DISCOUNT PIC 99.
PROCEDURE DIVISION.
MOVE "SILVER" TO WS-TYPE MOVE 650 TO WS-TOTAL
PERFORM FIND-DISCOUNT
MOVE "GOLD" TO WS-TYPE MOVE 80 TO WS-TOTAL
PERFORM FIND-DISCOUNT
MOVE "BRONZE" TO WS-TYPE MOVE 300 TO WS-TOTAL
PERFORM FIND-DISCOUNT
STOP RUN.
FIND-DISCOUNT.
EVALUATE WS-TYPE ALSO TRUE
WHEN "GOLD" ALSO ANY
MOVE 15 TO WS-DISCOUNT
WHEN "SILVER" ALSO WS-TOTAL >= 500
MOVE 10 TO WS-DISCOUNT
WHEN "SILVER" ALSO ANY
MOVE 5 TO WS-DISCOUNT
WHEN ANY ALSO WS-TOTAL >= 1000
MOVE 5 TO WS-DISCOUNT
WHEN OTHER
MOVE 0 TO WS-DISCOUNT
END-EVALUATE
DISPLAY WS-TYPE " " WS-TOTAL " -> " WS-DISCOUNT "%".SILVER 00650 -> 10%
GOLD 00080 -> 15%
BRONZE 00300 -> 00%
Note:
WHEN OTHERis optional. If no branch matches and there is noWHEN OTHER, nothing happens and execution continues afterEND-EVALUATE. Including it is good practice, even if it only displays an error.
Test yourself
If two WHEN branches both match, which statements run?
- Both
- Only the first matching branch
- Only the last matching branch
- None, it is an error
What does ANY mean in a WHEN with ALSO?
- Matches any value for that subject
- Matches no value
- Means OR
- Ends the EVALUATE
What does EVALUATE TRUE do?
- Always runs WHEN OTHER
- Runs the first WHEN whose condition is true
- Tests a boolean variable called TRUE
- Loops until a condition is true