The following formation and evaluation rules aplly to arithmetic expressions:
- Parentheses can be used in arithmetic expressions to specify the order in which elements are to be evaluated. Expressions
within parentheses are evaluated first, and 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:
|Unary plus and minus, B-NOT
|Multiplication and division
|Addition and subtraction
|B-XOR and B-EXOR
Thus, in the expression:
1 + 2 * 3
the order of evaluation is:
- 2 * 3
yielding an intermediate result of 6
- 1 + 6
giving a result of 7.
- 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.
- An arithmetic expression can only begin with the symbol "(", "+", "-", the bitwise operator B-NOT, or a variable, and can
only end with a ")" or a variable. There must be a one-to-one correspondence between left and right parenthesis of an arithmetic
expression such that each left parenthesis is to the left of its corresponding right parenthesis.
- Arithmetic expressions allow you to combine arithmetic operations without the restrictions on composite of operands and/or
receiving data items. See, for example, Syntax Rule 3 of The ADD Statement in this chapter.