Conditionals: IF & EVALUATE
14.1 IF / ELSE / END-IF
Always close with END-IF. A stray period inside an IF ends the whole statement — the classic COBOL bug:
IF WS-BALANCE < 0
DISPLAY "Overdrawn"
ADD 1 TO WS-OVERDRAWN-COUNT
ELSE
IF WS-BALANCE > 10000
DISPLAY "Premium customer"
ELSE
DISPLAY "Regular customer"
END-IF
END-IF* BUG: the period after the first DISPLAY ends the IF,
* so "ADD 1" executes for every record!
IF WS-AMOUNT > 1000
DISPLAY "Large amount".
ADD 1 TO WS-LARGE-COUNT14.2 Kinds of conditions
IF A > B, IF A IS GREATER THAN OR EQUAL TO B, IF A NOT = B
IF WS-INPUT IS NUMERIC, ALPHABETIC, ALPHABETIC-UPPER, ALPHABETIC-LOWER
IF WS-AMT IS POSITIVE, NEGATIVE, ZERO Condition name (88) IF END-OF-FILE, IF NOT VALID-STATUS IF A > 0 AND (B = 1 OR C = 2)
IF WS-CODE = "A" OR "B" OR "C" (subject and operator implied)
The NUMERIC class test is essential for validating input before arithmetic — non-numeric data in a numeric field causes the infamous S0C7 data exception abend on z/OS.
14.3 EVALUATE — COBOL's powerful case statement
WHEN clauses are tested in order; the first match wins (no fall-through).
Consecutive WHENs with no statements share the next statement group (like WHEN 6 above).
ANY matches anything; NOT negates a value (WHEN NOT "X").
* Evaluate a single subject
EVALUATE WS-DAY-NUM
WHEN 1 THRU 5 DISPLAY "Weekday"
WHEN 6
WHEN 7 DISPLAY "Weekend"
WHEN OTHER DISPLAY "Invalid day"
END-EVALUATE
* EVALUATE TRUE: a clean replacement for nested IFs
EVALUATE TRUE
WHEN WS-SCORE >= 90 MOVE "A" TO WS-GRADE
WHEN WS-SCORE >= 80 MOVE "B" TO WS-GRADE
WHEN WS-SCORE >= 70 MOVE "C" TO WS-GRADE
WHEN WS-SCORE >= 60 MOVE "D" TO WS-GRADE
WHEN OTHER MOVE "F" TO WS-GRADE
END-EVALUATE
* Multiple subjects with ALSO: a decision table
EVALUATE WS-CUST-TYPE ALSO TRUE
WHEN "G" ALSO WS-ORDER-AMT > 1000
MOVE 0.15 TO WS-DISCOUNT
WHEN "G" ALSO ANY
MOVE 0.10 TO WS-DISCOUNT
WHEN "R" ALSO WS-ORDER-AMT > 1000
MOVE 0.05 TO WS-DISCOUNT
WHEN OTHER
MOVE 0 TO WS-DISCOUNT
END-EVALUATE14.4 CONTINUE and NEXT SENTENCE
CONTINUE is a no-operation placeholder (IF A = B CONTINUE ELSE ...). NEXT SENTENCE jumps past the next period — dangerous with scope terminators; avoid it in new code.