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
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.
- 1Identify the tie
Both operators are subtraction operators, so they have equal precedence.
- 2Evaluate from the left
The leftmost subtraction is 5 - 3, which produces 2.
- 3Continue with the remaining expression
After the first operation, the expression becomes 2 - 1.
- 4Evaluate 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 |
Different grouping changes the result of subtraction.
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).
- 1Evaluate without parentheses
The operators have equal precedence, so evaluate from left to right: 10 - 5 produces 5, then 5 + 2 produces 7.
- 2Evaluate 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
Common mistake Reading an equal-precedence expression from right to left.
What it looks likeTreating 5 - 3 - 1 as 5 - (3 - 1).Why it failsEqual-precedence operators are evaluated from left to right, not right to left.
Do this insteadEvaluate 5 - 3 first, then subtract 1 from the result.
Common mistake Assuming subtraction can be regrouped without changing the result.
What it looks likeAssuming (5 - 3) - 1 and 5 - (3 - 1) are equivalent.
Why it failsSubtraction is not associative, so different grouping produces different results.
Do this insteadKeep the default left-to-right grouping or use parentheses to specify the grouping you need.
Common mistake Ignoring parentheses when determining the first operation.
What it looks likeEvaluating 10 - (5 + 2) as though it were 10 - 5 + 2.Why it failsParentheses force their enclosed sub-expression to be evaluated first.
Do this insteadEvaluate 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
- 1Operators with equal precedence are evaluated from left to right.
- 2In 5 - 3 - 1, compute 5 - 3 first, then compute 2 - 1, giving 1.
- 3Subtraction and division are not associative, so changing the grouping can change the result.
- 4Parentheses override left-to-right evaluation and force a chosen sub-expression to be evaluated first.
- 5Tracing 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.