Conditions and relation tests

Relational tests

COBOL compares with =, NOT =, GREATER THAN, LESS THAN, and their abbreviations (>, <, >=, <=). Tests appear inside IF and EVALUATE sentences:

IF WS-HOURS > 40 COMPUTE WS-PAY = WS-HOURS * WS-RATE * 1.5 END-IF.

Level 88 names

Level 88 gives a name to a value or range. The condition name then reads like a flag in IF sentences:

01 WS-STATUS PIC X. 88 WS-OK VALUE "A". 88 WS-ERROR VALUE "E" "F".

Class tests

CLASS tests check the shape of a field: NUMERIC, ALPHABETIC, ALPHABETIC-UPPER, and ALPHABETIC-LOWER. They are the safe way to validate input before arithmetic:

IF WS-INPUT IS NUMERIC MOVE WS-INPUT TO WS-COUNT END-IF.

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