COBOL Arithmetic Expressions
An arithmetic expression is a user-defined formula that performs arithmetic calculations. These are combinations of variables and numbers separated by arithmetic operators.
Arithmetic expressions may contain any of the following –
- Variables declared as numeric.
- Numeric values.
- Arithmetic operators.
- An arithmetic expression is enclosed in parentheses.
For example,
 WORKING-STORAGE SECTION.
 01 WS-VAR.
    05 WS-INPUT      PIC 9(03) VALUE 20.
    05 WS-OUTPUT     PIC 9(03).
 PROCEDURE DIVISION.
	 COMPUTE WS-OUTPUT = WS-INPUT + 10.In the above example, WS-OUTPUT = WS-INPUT + 10 is the arithmetic expression.
Rules to Remember -
- All variables or values should be of type numeric.
- SPACE should separate each parameter in the expression (variables, operators, etc.).
Let us discuss about COBOL arithmetic operators to understand the arithmetic expression in detail.
Arithmetic Operators -
All arithmetic operators are majorly divided into two types and those are –
- Unary Operators
- Binary Operators
Unary Operators -
Unary operators are arithmetic operators that perform an action on a single operand. There are two unary operators, negative ( - ) and positive ( + ). The negative unary operator reverses the sign of an expression from positive to negative or negative to positive.
| Unary operator | Meaning | 
|---|---|
| + | Multiplication by +1 | 
| - | Multiplication by -1 | 
Binary Operators -
A binary operation is an operation that requires two inputs. These inputs are known as operands in mathematical terminology. The binary operations are - addition, multiplication, subtraction, and division.
| Binary operator | Meaning | Equalent Statement | 
|---|---|---|
| + | Addition | ADD Statement | 
| - | Subtraction | SUBTRACT Statement | 
| * | Multiplication | MULTIPLY Statement | 
| / | Division | DIVIDE Statement | 
| ** | Exponentiation (In some COBOL extensions) | 
Arithmetic operator's execution order -
The evaluation order of operators is -
- Expressions within parentheses → ( ).
- Unary operator → +, -.
- Exponentiation → **
- Multiplication and division → *, /.
- Binary addition and subtraction → +, -.
Arithmetic Statements -
The COBOL arithmetic statements are -
Arithmetic with date fields -
Arithmetic operations on a date field are restricted to -
- Adding days to a date field.
- Subtracting days from a date field.
- Subtracting a date field from a compatible date field.
The following operations are not allowed -
- Any operation between incompatible dates.
- Adding two date fields if they are not compatible.
- Subtracting a date field from a nondate.
- Unary minus, division, exponentiation, or multiplication of or by a date field.
Results of using date fields in addition -
| Nondate second operand | Date field second operand | |
|---|---|---|
| Nondate first operand | Nondate | Date field | 
| Date field first operand | Date field | Not allowed | 
Results of using date fields in subtraction -
| Nondate second operand | Date field second operand | |
|---|---|---|
| Nondate first operand | Nondate | Not allowed | 
| Date field first operand | Date field | Nondate | 
