COBOL GnuCOBOL 3.2.0 Β·
β verified by execution on 2026-08-16
When programs require complex conditional logic, nested IF-ELSE statements can quickly become difficult to read. Mainstream languages like C, Java, and JavaScript introduced the switch statement to handle this.
COBOL introduced the EVALUATE statement in 1985. It serves the same purpose as a switch statement but is significantly more powerful.
The Basic EVALUATE
In its simplest form, EVALUATE checks a single variable against multiple potential values using WHEN clauses. The WHEN OTHER clause acts as the default fallback if no preceding condition is met.
cobolβ verified output
IDENTIFICATION DIVISION. PROGRAM-ID. EVAL-BASIC. DATA DIVISION. WORKING-STORAGE SECTION. 01 HTTP-STATUS PIC 999 VALUE 404. PROCEDURE DIVISION. EVALUATE HTTP-STATUS WHEN 200 DISPLAY 'OK' WHEN 404 DISPLAY 'NOT FOUND' WHEN 500 DISPLAY 'SERVER ERROR' WHEN OTHER DISPLAY 'UNKNOWN STATUS' END-EVALUATE. STOP RUN.
Output
NOT FOUND
Your output
No Fall-Through
If you have experience with C, C++, or Java, you are likely familiar with the concept of βfall-through.β In those languages, if you omit a break statement at the end of a case, execution blindly continues into the next case.
COBOL does not have fall-through. Once a WHEN clause is matched and its instructions are executed, control immediately jumps to END-EVALUATE.
Predict the outputcobol
Read the code. What exactly will it print? Commit to an answer before you look.
IDENTIFICATION DIVISION. PROGRAM-ID. EVAL-FALL. DATA DIVISION. WORKING-STORAGE SECTION. 01 LOG-LEVEL PIC X VALUE 'W'. PROCEDURE DIVISION. EVALUATE LOG-LEVEL WHEN 'W' DISPLAY 'WARNING' WHEN 'E' DISPLAY 'ERROR' END-EVALUATE. DISPLAY 'DONE'. STOP RUN.
Output
WARNING
DONE
You predicted
A novice coming from C might expect this to output WARNING, ERROR, and DONE because there is no break statement. However, in COBOL, the EVALUATE statement automatically exits the block after executing the matched WHEN clause!
EVALUATE TRUE: The Decision Table
The most celebrated feature of COBOLβs EVALUATE statement is EVALUATE TRUE.
Instead of passing a variable to EVALUATE, you pass the boolean literal TRUE. This allows each WHEN clause to be an entirely independent boolean expression, functioning like a clean IF-ELSE-IF chain.
cobolβ verified output
IDENTIFICATION DIVISION. PROGRAM-ID. EVAL-TRUE. DATA DIVISION. WORKING-STORAGE SECTION. 01 AGE PIC 99 VALUE 70. PROCEDURE DIVISION. EVALUATE TRUE WHEN AGE < 13 DISPLAY 'CHILD' WHEN AGE >= 13 AND AGE < 20 DISPLAY 'TEENAGER' WHEN AGE >= 65 DISPLAY 'SENIOR' WHEN OTHER DISPLAY 'ADULT' END-EVALUATE. STOP RUN.
Output
SENIOR
Your output
Because EVALUATE exits upon the first match, order matters. The conditions are evaluated top-to-bottom. If AGE is 70, the first two conditions evaluate to FALSE. The third evaluates to TRUE, executes DISPLAY 'SENIOR', and then drops out of the EVALUATE block.
Visualization: The Evaluation Flow
Here is a visual representation of how EVALUATE TRUE cleanly routes execution. Notice how it exits immediately after a match, avoiding the spaghetti flow of nested IF statements.
Check yourself
How does the EVALUATE statement handle fall-through?
Reveal answer
It never falls through; once a WHEN clause executes, control jumps to END-EVALUATE. β COBOL's EVALUATE implicitly breaks after a match. There is no fall-through mechanism.
What is the purpose of EVALUATE TRUE?
Reveal answer
It allows testing of completely independent boolean conditions in each WHEN clause. β EVALUATE TRUE acts as a highly readable alternative to nested IF-ELSE structures, treating each WHEN clause as an independent boolean test.
If the first two WHEN clauses in an EVALUATE block evaluate to TRUE, which ones execute?
Reveal answer
Only the first one executes. β COBOL EVALUATE exits immediately after the very first matched WHEN clause. Subsequent matches are never evaluated.
Challenges
Challenge 1 +20 XP
Write a basic EVALUATE that checks the COMMAND variable. If it is 'START', display 'SYSTEM BOOTING'. If it is 'STOP', display 'SYSTEM HALTED'. For any other command, display 'INVALID COMMAND'.
cobolβ solution verified at build time
COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the
fixed code should be, then open the solution and compare. The expected output
below is real β it came from compiling the solution with GnuCOBOL when this page
was built.
Test 1 β expects "SYSTEM HALTED\n"
Need a hint? (β25% XP)
Use `WHEN 'START'` and `WHEN 'STOP '`. Use `WHEN OTHER` for the invalid command case.
Show solution (0 XP)
IDENTIFICATION DIVISION.PROGRAM-ID. CMD-TEST.DATA DIVISION.WORKING-STORAGE SECTION.01 COMMAND PIC X(5) VALUE 'STOP '.PROCEDURE DIVISION. EVALUATE COMMAND WHEN 'START' DISPLAY 'SYSTEM BOOTING' WHEN 'STOP ' DISPLAY 'SYSTEM HALTED' WHEN OTHER DISPLAY 'INVALID COMMAND' END-EVALUATE. STOP RUN.
Challenge 2 +20 XP
Convert this logic to use EVALUATE TRUE. If SCORE is 90 or above, display 'A'. If 80 or above, display 'B'. Otherwise, display 'C'.
cobolβ solution verified at build time
COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the
fixed code should be, then open the solution and compare. The expected output
below is real β it came from compiling the solution with GnuCOBOL when this page
was built.
Test 1 β expects "B\n"
Need a hint? (β25% XP)
Use `WHEN SCORE >= 90`, then `WHEN SCORE >= 80`, and finally `WHEN OTHER`.
Show solution (0 XP)
IDENTIFICATION DIVISION.PROGRAM-ID. GRADE-TEST.DATA DIVISION.WORKING-STORAGE SECTION.01 SCORE PIC 99 VALUE 85.PROCEDURE DIVISION. EVALUATE TRUE WHEN SCORE >= 90 DISPLAY 'A' WHEN SCORE >= 80 DISPLAY 'B' WHEN OTHER DISPLAY 'C' END-EVALUATE. STOP RUN.