COBOL has two kinds of expression: arithmetic (a number) and conditional (true or false). Both are written in words and symbols together and both are evaluated with decimal rules, not binary floating point, unless an item is a floating-point usage.
Arithmetic expressions
| Operator | Meaning | Precedence |
|---|---|---|
unary + - | Sign of an operand | 1 (highest) |
** | Power | 2 |
* / | Multiply, divide | 3 |
binary + - | Add, subtract | 4 |
- Operators of the same level run left to right; parentheses override. Binary operators are surrounded by spaces
(
A-Bis one word). - Operands are numeric items, numeric literals,
FUNCTIONcalls that return numbers, and other expressions. COMPUTEtakes a whole expression.ADD,SUBTRACT,MULTIPLY,DIVIDEtake simple operands and, in theGIVINGform, store the result in items that need not be numeric-edited sources.- Intermediate results are kept at the precision the compiler chooses; the standard
requires 31 digits in 2002 and later (IBM
ARITH(EXTEND); 18 digits withARITH(COMPAT)). Fixed-point decimal results are exact; there is no rounding unless asked. ROUNDEDrounds the result stored into an item with fewer decimals: half away from zero by default;MODE IS AWAY-FROM-ZERO,NEAREST-AWAY-FROM-ZERO,NEAREST-EVEN,NEAREST-TOWARD-ZERO,PROHIBITED,TOWARD-GREATER,TOWARD-LESSER,TRUNCATION(2002).ON SIZE ERRORcatches a result too large for its field, or division by zero. Without it the stored value is truncated (or undefined, on division by zero).
COMPUTE NET ROUNDED MODE IS NEAREST-EVEN
= GROSS * (1 - TAX-RATE) - FEE ** 2 / 100
ON SIZE ERROR DISPLAY "TOO BIG"
END-COMPUTE
DIVIDE TOTAL BY COUNT-ITEMS GIVING AVG REMAINDER LEFTOVER
2002 also lets an arithmetic expression use the FUNCTIONs of
Intrinsic functions directly (FUNCTION SQRT(X), FUNCTION MAX(A, B, C)).
Relation conditions
A relation compares two operands: A = B, A NOT = B, A < B, A > B, A <= B,
A >= B, A <> B or in words IS EQUAL TO, IS NOT EQUAL TO, IS GREATER THAN,
IS LESS THAN, IS GREATER THAN OR EQUAL TO, IS LESS THAN OR EQUAL TO. NOT negates the operator.
- Numeric operands are compared by algebraic value, whatever their usage and size.
- Alphanumeric operands are compared character by character in the collating sequence
(ASCII or EBCDIC, or one named by
PROGRAM COLLATING SEQUENCE). The shorter operand is padded on the right with spaces. - A numeric item may be compared with an alphanumeric literal only when the item is an integer displayed as characters.
- Index and pointer operands support equality (pointer:
= NULL) and index relations. - National and boolean operands compare by their code values.
- Floating-point and decimal-float operands compare by value (
FLOAT-NOT-A-NUMBERandFLOAT-INFINITYare special in 2014).
Class, sign, condition-name and switch conditions
| Form | True when |
|---|---|
x IS [NOT] NUMERIC | x contains only digits (with a valid sign for a signed item) |
x IS [NOT] ALPHABETIC | Letters and spaces only; ALPHABETIC-LOWER and ALPHABETIC-UPPER restrict the case |
x IS [NOT] class-name | x holds only characters of a class defined with CLASS in SPECIAL-NAMES |
x IS [NOT] BOOLEAN (2002) | x holds only 0 and 1 characters |
x IS [NOT] POSITIVE | NEGATIVE | ZERO | The sign test; x may be an arithmetic expression |
cond-name (level 88) | The item above equals one of the values or falls in a range of the 88 entry |
switch-name / mnemonic IS ON | OFF | An implementor switch (UPSI switches on IBM, SWITCH-n in GnuCOBOL) is set |
x IS [NOT] OMITTED | The linkage item was omitted by the caller (CALL ... USING OMITTED) |
x IS [NOT] OF CLASS / IS INSTANCE OF (2002) | An object is an instance of a class |
EXCEPTION-STATUS, ADDRESS OF x IS [NOT] NULL, x IS [NOT] EQUAL TO NULL | The 2002 exception state; pointer tests |
Compound and abbreviated conditions
AND,ORandNOTcombine conditions. Precedence:NOT, thenAND, thenOR; parentheses override. The 2023 standard addsXOR(exclusive or) and boolean operators on bit items:B-AND,B-OR,B-XOR,B-NOT,B-SHIFT-L,B-SHIFT-R,B-SHIFT-LC,B-SHIFT-RC.- Abbreviated relations omit a repeated subject or operator:
IF A > B AND CmeansA > B AND A > C;IF A = 1 OR 2 OR 3meansA = 1 OR A = 2 OR A = 3;IF A NOT = B OR CmeansNOT = BandNOT = Ccarried through. The rule is hard to read, and many shops forbid it. - Conditions appear in
IF,EVALUATE,PERFORM UNTIL,SEARCH WHEN,WHENofEVALUATE, andVALIDATErules.
IF (STATUS-CODE = "A" OR "B") AND AMOUNT > ZERO
AND NOT (REGION = "N" OR REGION = "S")
PERFORM PROCESS-IT
END-IF
IF NAME-FIELD IS ALPHABETIC AND ID-FIELD IS NUMERIC
SET RECORD-OK TO TRUE
END-IF
MOVE: conversions between categories
MOVE converts as it copies. Rows are the sending item, columns the receiving item:
| From \ To | Alphanumeric | Numeric (integer or decimal) | Numeric-edited | Alphabetic |
|---|---|---|---|---|
| Alphanumeric | Yes. Left-justified and space-padded, or truncated on the right (JUSTIFIED RIGHT reverses). | Allowed for an alphanumeric item that holds an integer; the digits are moved as numbers | Allowed for integer text | Yes |
| Numeric integer | Yes: the digits (without sign or point) as characters | Yes. Aligned on the decimal point; extra digits truncated left and right | Yes: edited | No |
| Numeric non-integer | No | Yes | Yes | No |
| Numeric-edited | Yes | Yes: de-edited | Yes | No |
| Figurative constant | Filled with the character | ZERO only | ZERO or SPACE | SPACE |
Group items are alphanumeric for a MOVE: the bytes are copied without conversion, which is the main source of
errors. MOVE CORRESPONDING moves each subordinate item that has the same name in both groups. A
MOVE of a numeric item to an unsigned item drops the sign. A move between binary items obeys the truncation
option (TRUNC, binary-truncate), so storing 12345 in PIC 9(4) BINARY may store 2345 or 12345.
Other expression forms
- Reference modification:
X(3:4),X(I:), and on the result of a function:FUNCTION TRIM(X)(1:3)(2002). - Concatenation:
"abc" & "def"of literals;FUNCTION CONCATENATE(a, b)for values;STRINGfor items. - Address and length:
ADDRESS OF x,LENGTH OF x,FUNCTION LENGTH(x),FUNCTION BYTE-LENGTH(x). - Qualified names, subscripts, indexes: see Data division.
- Object expressions:
obj::"method"(args), the inline method invocation of IBM Enterprise COBOL and the 2002 standard; see Object-oriented COBOL.