Operators in COBOL
Learn arithmetic, comparison, and logical operators in COBOL - including the verbose verb-based syntax (ADD, SUBTRACT, MULTIPLY, DIVIDE) and the modern COMPUTE statement
COBOL takes a unique approach to operators that reflects its business-documentation philosophy. Where most languages use compact symbols like +, -, *, and /, COBOL offers two parallel styles: verbose English-like verbs (ADD, SUBTRACT, MULTIPLY, DIVIDE) and the more familiar symbolic syntax through the COMPUTE statement.
This dual nature is intentional. A 1960s payroll clerk reviewing source code could read MULTIPLY HOURS BY RATE GIVING GROSS-PAY as a business rule, while a modern programmer can write COMPUTE GROSS-PAY = HOURS * RATE for compactness. Both compile to the same instructions.
In this tutorial you will learn how COBOL handles arithmetic, comparisons, and logical conditions. You will also see how operator precedence works in COMPUTE, how rounding and overflow are handled with ROUNDED and ON SIZE ERROR, and how COBOL’s wordy comparisons (IS GREATER THAN, IS EQUAL TO) coexist with the familiar symbols (>, =, <).
Arithmetic with Verbs
COBOL’s original arithmetic style uses dedicated verbs for each operation. Each verb has a GIVING clause that names the result field, leaving the source operands unchanged.
Create a file named arithm.cob:
| |
Notice that DIVIDE accepts an optional REMAINDER clause - a built-in way to get integer division and modulo in a single statement.
The COMPUTE Statement
For complex expressions, the verb-based syntax becomes cumbersome. The COMPUTE statement uses familiar infix operators: +, -, *, /, and ** (exponentiation). Standard mathematical precedence applies, and parentheses force evaluation order.
Create a file named compute.cob:
| |
The ROUNDED keyword rounds the result to the precision of the target field rather than truncating. Without it, 35 / 3 would store 11.66 (truncated) instead of 11.67 (rounded).
Comparison and Logical Operators
COBOL supports both wordy and symbolic comparisons, and they are completely interchangeable. The verbose forms (IS GREATER THAN, IS EQUAL TO, IS NOT LESS THAN) read like English, while the symbolic forms (>, =, <) are familiar to programmers from other languages.
Logical conditions use the keywords AND, OR, and NOT. There is no short-circuit guarantee in standard COBOL, so both sides of a logical expression may be evaluated.
Create a file named compare.cob:
| |
COBOL also supports class condition tests like IS NUMERIC, IS ALPHABETIC, and IS POSITIVE. These let you check the contents of a field without an explicit comparison: IF AGE IS NUMERIC is true when every character in AGE is a digit.
Handling Arithmetic Errors
Because COBOL fields have fixed precision defined by their PIC clauses, a result that exceeds the target’s size is a real concern in business code. The ON SIZE ERROR clause lets you handle overflow without crashing the program.
Create a file named safemath.cob:
| |
ON SIZE ERROR catches two conditions: a result too large to fit in the target field, and division by zero. The END-COMPUTE and END-DIVIDE scope terminators (introduced in COBOL 85) close the statement explicitly, which is the modern style.
Running with Docker
| |
Expected Output
Running arithm:
ADD : 00029
SUBTRACT : 00021
MULTIPLY : 00100
DIVIDE : 00006 REMAINDER 001
Running compute:
Simple interest : 0000150.00
Compound total : 0001157.63
Average : 021.67
Running compare:
Adult (verbose form)
Adult (symbolic form)
Qualifies for premium tier
Account is active or pending
Not a minor
Running safemath:
Overflow: result too large for field
Division error (likely divide by zero)
Operator Precedence
When using COMPUTE, COBOL follows standard mathematical precedence:
| Precedence | Operators | Description |
|---|---|---|
| 1 (highest) | ** | Exponentiation |
| 2 | unary + - | Sign |
| 3 | * / | Multiplication, division |
| 4 | + - | Addition, subtraction |
Within the same precedence level, evaluation is left-to-right. When in doubt - especially in financial calculations - use parentheses. They cost nothing and make business rules explicit to the next person who reads the code.
Key Concepts
- Two arithmetic styles coexist: verbose verbs (
ADD,MULTIPLY) read like English;COMPUTEuses familiar infix operators including**for exponentiation. GIVINGkeeps operands intact:ADD A TO B GIVING CleavesAandBunchanged. WithoutGIVING, the result lands in the second operand.ROUNDEDcontrols fractional results: precision is governed by the target field’sPICclause; withoutROUNDED, extra digits are truncated.- Comparisons are dual-form:
IS GREATER THANand>mean the same thing; use whichever fits the audience reading the code. ON SIZE ERRORhandles overflow and divide-by-zero without aborting the program - critical for long-running batch jobs.- Class conditions like
IS NUMERICandIS ALPHABETICtest field contents without a comparison expression. - Logical operators are wordy:
AND,OR, andNOT- no&&or||. Standard COBOL does not guarantee short-circuit evaluation. - Scope terminators (
END-COMPUTE,END-IF,END-DIVIDE) make modern COBOL safer than the original period-terminated style.
Running Today
All examples can be run using Docker:
docker pull esolang/cobol:latest
Comments
Loading comments...
Leave a Comment