IF, ELSE and EVALUATE

IF END-IF

IF runs a sentence when its condition is true. END-IF closes the block, and periods belong to the last sentence inside, not to END-IF:

IF WS-SCORE >= 90 MOVE "A" TO WS-GRADE END-IF.

ELSE

ELSE adds the false path. Nest IF sentences inside each other, and keep the indentation one level per block so END-IF matches its IF:

IF WS-SCORE >= 90 MOVE "A" TO WS-GRADE ELSE IF WS-SCORE >= 80 MOVE "B" TO WS-GRADE END-IF END-IF.

EVALUATE

EVALUATE is the case statement. WHEN matches a value or condition, and WHEN OTHER catches everything else. It replaces deep nesting with a flat list:

EVALUATE WS-GRADE WHEN "A" DISPLAY "Excellent" WHEN "B" DISPLAY "Good" WHEN OTHER DISPLAY "Retry" END-EVALUATE.

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

Article author: Arthur Isaev