Arithmetic Operators in Programming
When operators have equal precedence, they are evaluated from left to right, not right to left.
When Operators Tie
When two or more operators in an expression have equal precedence, the expression is evaluated from left to right. This rule determines which operation happens first when the operators have the same level of priority.
What do you think happens?
What is the result of 5 - 3 - 1?
Reveal answer
Answer: 1
The subtraction operators have equal precedence, so evaluation proceeds from left to right: 5 - 3 is evaluated first, producing 2, and then 2 - 1 produces 1.
Spotting Equal Precedence
The first step is to identify whether the operators in an expression have equal precedence. In 5 - 3 - 1, both operators are subtraction operators, so they have equal precedence. Neither subtraction is selected because it is mathematically more important than the other; the tie is resolved by reading from left to right.
Tracing the First Operation
Evaluating 5 - 3 - 1
Determine the result of 5 - 3 - 1 using the equal-precedence rule.
Identify the tie: Both operators are subtraction operators, so they have equal precedence.
Evaluate from the left: The leftmost subtraction is 5 - 3, which produces 2.
Continue with the remaining expression: After the first operation, the expression becomes 2 - 1.
Evaluate the remaining operation: Compute 2 - 1 to obtain 1.
5 - 3 - 1 equals 1.
Why Grouping Matters
Left-to-right evaluation is especially important for subtraction and division because these operators are not associative. Grouping changes the result: (5 - 3) - 1 equals 1, while 5 - (3 - 1) equals 3. Without a rule for resolving the order, an expression such as 5 - 3 - 1 would be ambiguous.
| Grouping | First operation | Result |
|---|---|---|
| (5 - 3) - 1 | 5 - 3 | 1 |
| 5 - (3 - 1) | 3 - 1 | 3 |
Parentheses as Control
Parentheses override the usual left-to-right evaluation rule by forcing the enclosed sub-expression to be evaluated first. They let you state the intended grouping explicitly and control the order of evaluation.
Changing the Order Explicitly
Compare 10 - 5 + 2 with 10 - (5 + 2).
Evaluate without parentheses: The operators have equal precedence, so evaluate from left to right: 10 - 5 produces 5, then 5 + 2 produces 7.
Evaluate with parentheses: The parentheses force 5 + 2 to be evaluated first, producing 7. Then compute 10 - 7.
10 - 5 + 2 equals 7, while 10 - (5 + 2) equals 3.
Mistakes to Avoid
Reading an equal-precedence expression from right to left.
Equal-precedence operators are evaluated from left to right, not right to left.
Fix:
Evaluate 5 - 3 first, then subtract 1 from the result.Assuming subtraction can be regrouped without changing the result.
Subtraction is not associative, so different grouping produces different results.
Fix:
Keep the default left-to-right grouping or use parentheses to specify the grouping you need.Ignoring parentheses when determining the first operation.
Parentheses force their enclosed sub-expression to be evaluated first.
Fix:
Evaluate 5 + 2 first, then subtract the result from 10.
Practice the Trace
Trace each expression from left to right where the operators have equal precedence. Then compare the result with the version that uses parentheses: 5 - 3 - 1 and 5 - (3 - 1).
Hints
- For 5 - 3 - 1, evaluate the leftmost subtraction first.
- For 5 - (3 - 1), evaluate the parenthesized subtraction first.
The Evaluation Rule
- Operators with equal precedence are evaluated from left to right.
- In 5 - 3 - 1, compute 5 - 3 first, then compute 2 - 1, giving 1.
- Subtraction and division are not associative, so changing the grouping can change the result.
- Parentheses override left-to-right evaluation and force a chosen sub-expression to be evaluated first.
- Tracing intermediate expressions makes the evaluation order clear and prevents calculation errors.
Key Takeaways
- Equal-precedence operators are evaluated from left to right.
- The expression 5 - 3 - 1 evaluates as (5 - 3) - 1, producing 1.
- Subtraction and division are not associative, so grouping matters.
- Parentheses override the default order and allow you to control which sub-expression is evaluated first.