# Arithmetic Expressions in COBOL COBOL

An arithmetic expression can be an identifier of a numeric elementary item, a numeric literal, such identifiers and literals separated by arithmetic operators, two arithmetic expressions separated by an arithmetic operator or an arithmetic expression enclosed in parentheses. Any arithmetic expression may be preceded by a unary operator.
The permissible combinations of variables, numeric literals, arithmetic operators and parentheses are given in Table.

Combination of Symbols in Arithmetic Expressions

Arithmetic Operators

There are five binary arithmetic operators and two unary arithmetic operators that may be used in arithmetic expressions. They are represented by specific characters that must be preceded by a space and followed by a space. See Table.

Arithmetic Operators

Formation and Evaluation Rules

Parentheses may be used in arithmetic expressions to specify the order in which elements are to be evaluated. Expressions within parentheses are evaluated first; within nested parentheses, evaluation proceeds from the least inclusive set to the most inclusive set. When parentheses are not used, or parenthesized expressions are at the same level of inclusiveness, the following hierarchical order of execution is implied:

1. Unary plus and minus
2. Exponentiation
3. Multiplication and division

Parentheses are used either to eliminate ambiguities in logic where consecutive operations of the same hierarchical level appear, or to modify the normal hierarchical sequence of execution in expressions where it is necessary to have some deviation from the normal precedence. When the sequence of execution is not specified by parentheses, the order of execution of consecutive operations of the same hierarchical level is from left to right.

The ways in which operands, operators and parentheses may be combined in an arithmetic expression are summarized in Table.

An arithmetic expression may begin only with one of the following symbols: ( + – or an operand. An arithmetic expression may end only with a ) or an operand. There must be a one-to-one correspondence between left and right parentheses in an arithmetic expression such that each left parenthesis is to the left of its corresponding right parenthesis.

The following rules apply to evaluation of exponentiation in the following arithmetic expression:

arithmetic-expression-1 ** arithmetic-expression-2

arithmetic-expression-1 provides the base value and arithmetic-expression-2 provides the exponent value.

1. If the value of the base is zero, the exponent value must be greater than zero; otherwise, the size error condition exists.
2. If the value of the base is negative, the exponent value must be an integer; otherwise, the size error condition exists.

Arithmetic expressions allow the user to combine arithmetic operations without the restrictions on composite of operands, receiving data items, or both.

0