EVALUATE TRUE and ALSO
EVALUATE with a field as its subject compares that field with values. Written as EVALUATE TRUE it takes conditions instead, which is how a program chooses between branches that ask different questions, and ALSO gives it a second subject so that two fields decide together.
EVALUATE TRUE
With TRUE as the subject, each WHEN is a condition, and the branch whose condition is true runs. This is the form that replaces a chain of IFs with different tests in them:
IDENTIFICATION DIVISION.
PROGRAM-ID. EVALTRUE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CODE-F PIC X VALUE "T".
01 AMT-F PIC 9(4) VALUE 250.
PROCEDURE DIVISION.
EVALUATE TRUE
WHEN CODE-F = "W" OR CODE-F = "T"
DISPLAY "withdrawal or transfer"
WHEN CODE-F = "D" AND AMT-F > 1000
DISPLAY "large deposit"
WHEN CODE-F = "D"
DISPLAY "deposit"
WHEN OTHER
DISPLAY "unknown code " CODE-F
END-EVALUATE.
STOP RUN.
cobc -x -free -Wall -o evaltrue evaltrue.cob
./evaltrue
withdrawal or transfer
Each WHEN here is a full condition with AND, OR and the parentheses of the previous lesson, and the first true one wins. The two deposit lines are in the order a reader needs: the large amount first, then the ordinary one, because a large deposit satisfies both conditions.
ALSO and two conditions
ALSO between two conditions in one WHEN means that both must be true. With EVALUATE TRUE ALSO TRUE the subject list is two TRUE values and each WHEN gives one condition per subject:
IDENTIFICATION DIVISION.
PROGRAM-ID. ALSO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 MARK-F PIC 9(3) VALUE 72.
01 ATT-F PIC X VALUE "G".
PROCEDURE DIVISION.
EVALUATE TRUE ALSO TRUE
WHEN MARK-F >= 90 ALSO ATT-F = "G"
DISPLAY "grade A"
WHEN MARK-F >= 50 ALSO ANY
DISPLAY "pass"
WHEN MARK-F >= 50 ALSO ATT-F = "P"
DISPLAY "pass, attendance problem"
WHEN OTHER
DISPLAY "fail"
END-EVALUATE.
STOP RUN.
cobc -x -free -Wall -o also also.cob
./also
pass
Two subjects and ALSO
The same word works with two fields as subjects: the first value of the WHEN is compared with the first subject, the one after ALSO with the second. A zone and a speed choose between three services:
IDENTIFICATION DIVISION.
PROGRAM-ID. SHIPPING.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 ZONE-F PIC X VALUE "N".
01 SPEED-F PIC 99 VALUE 5.
PROCEDURE DIVISION.
EVALUATE ZONE-F ALSO SPEED-F
WHEN "N" ALSO 1 THRU 3
DISPLAY "north, local"
WHEN "N" ALSO 4 THRU 9
DISPLAY "north, express"
WHEN "S" ALSO ANY
DISPLAY "south, any speed"
WHEN OTHER
DISPLAY "zone " ZONE-F " speed " SPEED-F " not covered"
END-EVALUATE.
STOP RUN.
cobc -x -free -Wall -o shipping shipping.cob
./shipping
north, express
The row is a pair and the statement picks the branch whose pair matches. This is how a program expresses a table of two keys without writing the four combinations as nested IFs, and it reads like the table a business analyst would draw.
ANY and the order of the WHENs
ANY matches whatever the subject holds, so it makes a branch depend on one subject only: the south zone is served at any speed. It is also the reason the order matters. In the grade program the branch WHEN MARK-F >= 50 ALSO ANY comes before the attendance branch, so a passing mark always prints pass and the attendance line never runs for a pass.
Written the other way round, with the attendance branch first, a mark of 72 with poor attendance would print pass, attendance problem. Both programs compile and both are correct COBOL; only one of them says what the school means. A reader cannot tell from the statement alone which branch is reachable, which is the one weakness this form shares with a long IF chain.
The next part of the course is the loop. It starts with PERFORM, which the programs on this page have already used twice: once to run a paragraph and once, inline, to repeat a group of statements.
Exercises
- Change CODE-F to D and AMT-F to 2500 and run the first program. Which branch runs, and why not the ordinary deposit?
- Swap the last two WHENs of the grade program and run it with ATT-F of P. What prints now?
- Add a zone "E" with a speed of 2 and a branch for it. What does the WHEN OTHER catch after that?
- Write the shipping table with nested IFs and count the lines. Which version would you give a new reader?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does EVALUATE TRUE change about the WHEN clauses?
Answer
They are conditions, and the first true one runs
TRUE is the subject, so each WHEN is evaluated as a condition and the first true branch wins.
-
What does ALSO mean inside one WHEN?
Answer
The condition for the next subject must match too
Each WHEN gives one value per subject, and ALSO separates the values of the two subjects.
-
What does ANY match?
Answer
Any value of the subject in that position
ANY makes the branch independent of that subject, as the south zone is served at any speed.
-
Why does the grade program print pass and never the attendance message?
Answer
The WHEN with ANY comes first and its condition is true
The first matching WHEN ends the search, so the later attendance branch is unreachable for a pass.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-F PIC 9(4) VALUE 150. PROCEDURE DIVISION. EVALUATE TRUE WHEN A-F IS GREATER THAN 100 DISPLAY "large" WHEN OTHER DISPLAY "small" END-EVALUATE. STOP RUN.Answer
large
The condition is true, so the first branch runs and WHEN OTHER is skipped.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 M-F PIC 9(3) VALUE 40. 01 A-F PIC X VALUE "G". PROCEDURE DIVISION. EVALUATE TRUE ALSO TRUE WHEN M-F IS GREATER THAN 50 ALSO A-F = "G" DISPLAY "pass" WHEN OTHER DISPLAY "fail" END-EVALUATE. STOP RUN.Answer
fail
The mark is 40, so the first condition of the pair is false and the pair does not match.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9 VALUE 9. PROCEDURE DIVISION. EVALUATE TRUE WHEN N-F = 1 DISPLAY "one" WHEN N-F = 2 DISPLAY "two" END-EVALUATE. DISPLAY "done". STOP RUN.Answer
done
Neither condition is true and there is no WHEN OTHER, so only the DISPLAY after the statement runs.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-F PIC 9(2) VALUE 3. PROCEDURE DIVISION. EVALUATE TRUE WHEN N-F IS LESS THAN 5 DISPLAY "small" WHEN N-F IS LESS THAN 10 DISPLAY "medium" WHEN OTHER DISPLAY "large" END-EVALUATE. STOP RUN.Answer
small
Three is less than five, and the first true condition ends the search.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q9. DATA DIVISION. WORKING-STORAGE SECTION. 01 Z-F PIC X VALUE "S". 01 S-F PIC 99 VALUE 7. PROCEDURE DIVISION. EVALUATE Z-F ALSO S-F WHEN "N" ALSO 1 THRU 3 DISPLAY "north local" WHEN "S" ALSO ANY DISPLAY "south" WHEN OTHER DISPLAY "other" END-EVALUATE. STOP RUN.Answer
south
The zone is S and ANY accepts any speed for it, so the south branch matches.
-
A program puts WHEN MARK >= 50 ALSO ANY before WHEN MARK >= 50 ALSO ATT = "P". What is the result?
Answer
The attendance branch is unreachable for every passing mark
The any-speed branch already matches the pair, and the search stops at the first match.
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