When you have more than one operator in an arithmetic expression,
the order of numbers and operators can be critical. For example, in
the following expression, which operation does the language processor
perform first?
7 + 2 * (9 / 3) - 1
Proceeding from left to right, the language processor evaluates
the expression as follows:
- First it evaluates expressions within parentheses.
- Then it evaluates expressions with operators of higher priority
before expressions with operators of lower priority.
Arithmetic operator priority is as follows, with the highest first:
Table 1. Arithmetic Operator PriorityOperator symbol |
Operator description |
- + |
Prefix operators |
** |
Power (exponential) |
* / % // |
Multiplication and division |
+ - |
Addition and subtraction |
Thus, the preceding example would be evaluated in the following
order:
- Expression in parentheses
7 + 2 * (9 / 3) - 1
\___/
3
- Multiplication
7 + 2 * 3 - 1
\___/
6
- Addition and subtraction from left to right
7 + 6 - 1 = 12