Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 38 ยท Procedures and Control Flow

EVALUATE: Multi-Way Decisions in COBOL

Use COBOL's EVALUATE like switch/case: WHEN values and ranges, WHEN OTHER, EVALUATE TRUE for conditions, ALSO for decision tables, with a safe no-fall-through behaviour.

When you have more than two outcomes, nested IF statements get deep and hard to read. EVALUATE is COBOL's multi-way branch. It plays the role of switch/case in other languages, but it is more powerful: the WHEN branches can test ranges and conditions, and you can evaluate several subjects at once to build a decision table. Only the first matching branch runs, and there is no fall-through.

Basic form: one subject

COBOL
           EVALUATE MONTH-NUMBER
               WHEN 1
                   MOVE "January" TO MONTH-NAME
               WHEN 2
                   MOVE "February" TO MONTH-NAME
               WHEN 3 THRU 5
                   MOVE "Spring" TO SEASON-NAME
               WHEN 6 THRU 8
                   MOVE "Summer" TO SEASON-NAME
               WHEN OTHER
                   MOVE "Unknown" TO MONTH-NAME
           END-EVALUATE
  • The subject (MONTH-NUMBER) is compared with each WHEN value in order.
  • THRU gives an inclusive range.
  • Several values for one branch: WHEN 12 WHEN 1 WHEN 2 (stacked WHEN lines share one body), or WHEN "A" THRU "C".
  • WHEN OTHER catches everything else. It must come last. Without it, an unmatched subject silently does nothing, so most programs include it.
  • Close with END-EVALUATE.

Stacking WHEN lines gives the same effect as grouping case labels:

COBOL
           EVALUATE DAY-OF-WEEK
               WHEN 6
               WHEN 7
                   DISPLAY "Weekend"
               WHEN OTHER
                   DISPLAY "Weekday"
           END-EVALUATE

EVALUATE TRUE: conditions instead of values

When each branch needs a different test, make TRUE the subject and write conditions as the WHEN entries:

COBOL
           EVALUATE TRUE
               WHEN SCORE >= 90
                   MOVE "A" TO GRADE
               WHEN SCORE >= 80
                   MOVE "B" TO GRADE
               WHEN SCORE >= 70
                   MOVE "C" TO GRADE
               WHEN OTHER
                   MOVE "F" TO GRADE
           END-EVALUATE

The branches are checked from top to bottom, so order the ranges from the strictest to the loosest. Level-88 names work too (WHEN ACCOUNT-CLOSED), which is why they pair so well with EVALUATE TRUE (see condition names).

Python Grade bands (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Two subjects with ALSO: decision tables

ALSO evaluates several subjects together; each WHEN supplies one value per subject, separated by ALSO. ANY matches anything.

COBOL
           EVALUATE CUSTOMER-TYPE ALSO ORDER-SIZE
               WHEN "VIP"     ALSO ANY
                   MOVE 20 TO DISCOUNT
               WHEN "REGULAR" ALSO 1 THRU 9
                   MOVE 0 TO DISCOUNT
               WHEN "REGULAR" ALSO 10 THRU 99
                   MOVE 5 TO DISCOUNT
               WHEN OTHER
                   MOVE 0 TO DISCOUNT
           END-EVALUATE

You can also use EVALUATE TRUE ALSO TRUE with a condition in each position:

COBOL
           EVALUATE TRUE ALSO TRUE
               WHEN IS-MEMBER ALSO TOTAL > 100
                   MOVE 10 TO DISCOUNT
               WHEN IS-MEMBER ALSO ANY
                   MOVE 5 TO DISCOUNT
               WHEN OTHER
                   MOVE 0 TO DISCOUNT
           END-EVALUATE

A table with ALSO replaces a pile of nested IF statements and makes missing combinations visible.

Python A decision table (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

No fall-through

Unlike C's switch, a COBOL WHEN branch ends when the next WHEN begins. You never write break. The first matching branch runs and control goes to the statement after END-EVALUATE. The only way to share a body is to stack WHEN lines with nothing between them, as with WHEN 6 and WHEN 7 above.

EVALUATE versus nested IF

Prefer EVALUATE when:

  • three or more outcomes depend on one field;
  • you map ranges to results (grades, tax bands, shipping tiers);
  • two or three conditions combine into a table.

Prefer IF when there are only two outcomes or when the tests are unrelated.

Common mistakes

  • Missing WHEN OTHER, so an unexpected value quietly skips the whole statement.
  • Ordering EVALUATE TRUE branches loosest-first (WHEN SCORE >= 70 before >= 90), so the broad branch always wins.
  • Putting a period inside an EVALUATE branch, which ends the whole sentence early.
  • Mismatching the number of ALSO parts between the subject and a WHEN.
  • Comparing a numeric subject to text values.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What happens after a WHEN branch finishes?

    COBOL has no fall-through. After the matching branch, execution continues after END-EVALUATE.
  2. 2. Which form lets each WHEN branch test a different condition?

    With TRUE as the subject, each WHEN supplies a condition and the first true one wins.
  3. 3. What does ANY mean in WHEN "VIP" ALSO ANY?

    ANY matches every value of that subject, so only the first subject is restricted.
  4. 4. How do you give two values the same branch body?

    Consecutive WHEN lines with nothing between them share the statements that follow.

Summary

  • EVALUATE subject WHEN value ... WHEN OTHER ... END-EVALUATE is COBOL's switch.
  • THRU gives ranges, stacked WHEN lines share a body, WHEN OTHER is the default.
  • EVALUATE TRUE tests conditions from top to bottom: put the strictest first.
  • ALSO builds multi-subject decision tables; ANY is a wildcard.
  • There is no fall-through and no break.

Next: PERFORM, Paragraphs and Sections