EVALUATE

EVALUATE is the COBOL case statement, and it is the statement that replaced the chains of IF that older programs are made of. It takes one subject, a list of WHEN values and an optional WHEN OTHER, and it does not use NOT anywhere, which is why it is harder to get wrong.

EVALUATE one subject

The subject follows the word EVALUATE, each WHEN names a value it may have, and the statements under that WHEN run when the value matches:

IDENTIFICATION DIVISION. PROGRAM-ID. MENU. DATA DIVISION. WORKING-STORAGE SECTION. 01 CHOICE-F PIC 9 VALUE 3. PROCEDURE DIVISION. EVALUATE CHOICE-F WHEN 1 DISPLAY "one: open a new account" WHEN 2 THRU 4 DISPLAY "two to four: show the balance" WHEN OTHER DISPLAY "unknown choice " CHOICE-F END-EVALUATE. STOP RUN.

cobc -x -free -Wall -o menu menu.cob
./menu

two to four: show the balance

WHEN OTHER catches everything the earlier values did not, and it is written last. The whole statement ends with END-EVALUATE, which is the scope terminator that the next lesson uses again with conditions instead of values.

WHEN with a range

A WHEN may name one value or one range, with THRU as in a level 88. Several values with a shared action need one WHEN each, or an EVALUATE TRUE with conditions, which is the next lesson:

IDENTIFICATION DIVISION. PROGRAM-ID. CODES. DATA DIVISION. WORKING-STORAGE SECTION. 01 CODE-F PIC X VALUE "T". PROCEDURE DIVISION. EVALUATE CODE-F WHEN "D" DISPLAY "deposit" WHEN "W" DISPLAY "withdrawal" WHEN "T" DISPLAY "transfer" WHEN "A" THRU "C" DISPLAY "early letter" WHEN OTHER DISPLAY "unknown code " CODE-F END-EVALUATE. STOP RUN.

cobc -x -free -Wall -o codes codes.cob
./codes

transfer

A WHEN "W" OR "T" is not the way to say this: the compiler reads the second word as the start of a condition and rejects the line with invalid conditional expression. A shared action becomes a paragraph performed from both WHENs, or an EVALUATE TRUE with the conditions written in full.

The first matching WHEN wins

The values are tried in order and the first match ends the search, even when a later WHEN would also match:

IDENTIFICATION DIVISION. PROGRAM-ID. ORDER. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 2. PROCEDURE DIVISION. EVALUATE N-F WHEN 1 THRU 5 DISPLAY "first: 1 through 5" WHEN 2 DISPLAY "second: exactly 2" WHEN OTHER DISPLAY "other" END-EVALUATE. STOP RUN.

cobc -x -free -Wall -o order order.cob
./order

first: 1 through 5

The value 2 matches both the range and the exact value, and the range is written first, so its statements run and the second WHEN is dead code. The compiler in this image does not report that; a reader has to see it. The way to avoid it is to put the narrowest value first, or to write the ranges so they cannot overlap.

A value that matches no WHEN

Without WHEN OTHER a value that matches nothing makes the statement do nothing at all, and the program continues with the next sentence:

IDENTIFICATION DIVISION. PROGRAM-ID. NOMATCH. DATA DIVISION. WORKING-STORAGE SECTION. 01 CODE-F PIC X VALUE "Z". PROCEDURE DIVISION. DISPLAY "before". EVALUATE CODE-F WHEN "A" DISPLAY "letter A" WHEN "B" DISPLAY "letter B" END-EVALUATE. DISPLAY "after". STOP RUN.

cobc -x -free -Wall -o nomatch nomatch.cob
./nomatch

before
after

No message, no error and no default branch. In a program that reads a code from a file this is the silent path that leaves a record unprocessed, which is why the course writes WHEN OTHER in every EVALUATE whose values come from outside the program, and makes it print the code it did not recognise or set a return status.

The IF chain and the EVALUATE

The same three cases, once as a nested IF and once as an EVALUATE. The output is the same line twice:

IDENTIFICATION DIVISION. PROGRAM-ID. SAME. DATA DIVISION. WORKING-STORAGE SECTION. 01 CODE-F PIC 9 VALUE 2. PROCEDURE DIVISION. IF CODE-F = 1 DISPLAY "one" ELSE IF CODE-F = 2 DISPLAY "two" ELSE DISPLAY "other" END-IF END-IF. EVALUATE CODE-F WHEN 1 DISPLAY "one" WHEN 2 DISPLAY "two" WHEN OTHER DISPLAY "other" END-EVALUATE. STOP RUN.

cobc -x -free -Wall -o same same.cob
./same

two
two

The EVALUATE is shorter, has no END-IF ladder and no indentation that decides what an ELSE belongs to, and each case is one line. The next lesson gives it conditions instead of values, which is what a program needs when the cases overlap or when one case asks two questions at once.

Exercises

  1. Change CHOICE-F to 1 and to 9 and run the menu program. Which line prints in each case?
  2. Put WHEN 2 before WHEN 1 THRU 5 in the order program. What prints now?
  3. Remove WHEN OTHER from the menu and run it with a choice of 9. What appears?
  4. Rewrite the four cases of the code program with an EVALUATE TRUE, where the last case is WHEN OTHER. Which version is easier to read?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does WHEN OTHER do in an EVALUATE?

    Answer

    It runs when no earlier WHEN matched, and is normally last

    The values are tried in order, and WHEN OTHER is the default branch for everything left over.

  2. Which WHEN runs when two of them could match?

    Answer

    The first one written, even if a later one also matches

    The order program printed the range first, and the exact value that also matched never ran.

  3. What happens when the subject matches no WHEN and there is no WHEN OTHER?

    Answer

    Nothing in the statement runs, and the program continues

    The run printed before and after with nothing between them, which is the silent path a program must not have.

  4. Why does WHEN "W" OR "T" fail to compile?

    Answer

    A WHEN takes one value or one range; a shared action needs a paragraph or EVALUATE TRUE

    The compiler reads the second word as a condition and reports an invalid conditional expression.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 C-F PIC 9 VALUE 2. PROCEDURE DIVISION. EVALUATE C-F WHEN 1 DISPLAY "one" WHEN 2 DISPLAY "two" WHEN OTHER DISPLAY "other" END-EVALUATE. STOP RUN.

    Answer

    two

    The subject is 2, so the second WHEN runs.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 C-F PIC 9 VALUE 7. PROCEDURE DIVISION. EVALUATE C-F WHEN 1 THRU 5 DISPLAY "low" WHEN 6 THRU 9 DISPLAY "high" WHEN OTHER DISPLAY "other" END-EVALUATE. STOP RUN.

    Answer

    high

    Seven is inside the second range and outside the first.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 C-F PIC X VALUE "Q". PROCEDURE DIVISION. EVALUATE C-F WHEN "A" DISPLAY "a" WHEN "B" DISPLAY "b" END-EVALUATE. DISPLAY "end". STOP RUN.

    Answer

    end

    No WHEN matches and there is no WHEN OTHER, so only the DISPLAY after the statement runs.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 C-F PIC X VALUE "B". PROCEDURE DIVISION. EVALUATE C-F WHEN "A" THRU "C" DISPLAY "early" WHEN "B" DISPLAY "exactly b" END-EVALUATE. STOP RUN.

    Answer

    early

    The range is written first and B is inside it, so the exact match never runs.

  9. A batch program reads a code from a file and its EVALUATE has no WHEN OTHER. What is the risk?

    Answer

    An unknown code passes with no message and no record processed

    Nothing runs for an unmatched value, so the record is silently skipped.

  10. What does END-EVALUATE do?

    Answer

    It ends the statement, so statements after it are unconditional

    It is the scope terminator of the statement, the same role END-IF has for an IF.

The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev