COBOL Manual for TNS and TNS/R Programs

Procedure Division
HP COBOL Manual for TNS and TNS/R Programs522555-006
8-47
Arithmetic Expressions
Formation and Evaluation Rules
Parentheses can be used to specify the order in which the elements of an expression
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 hierarchical order of execution is shown in Table 8-14.
When no parentheses are present to specify otherwise, the order of execution of
consecutive operations of the same hierarchical level is from left to right.
Table 8-15 shows some expressions that appear to be ambiguous and the COBOL
interpretation of them.
You can use arithmetic parentheses to:
Override the normal hierarchical sequence of execution in expressions whose
evaluation must not follow the normal precedence, for example:
A / ( B * C )
Clarify the hierarchical sequence of execution for the benefit of the reader, for
example:
PRIN + ( INT * PERIOD )
It is recommended that division be the last operation performed in an expression, if
possible; otherwise, execution can be slow and precision can be lost.
Table 8-14. Hierarchy of Operators
Hierarchy Operators
1st Unary plus and minus
2nd Exponentiation
3rd Multiplication and division
4th Addition and subtraction
Table 8-15. Precedence in Arithmetic Expressions
Ambiguous Interpretation
A / B
* C (A / B) * C
A / B / C (A / B) / C
A
** B ** C(A ** B) ** C
A + B / C + D
** E * F - G ((A + (B / C)) + ((D ** E) * F)) - G