Concepts / Arithmetic Operators in Programming

Arithmetic Operators in Programming

When operators have equal precedence, they are evaluated from left to right, not right to left.

  • Programming

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?

  • 1

  • 3

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.

5value-subtraction3value-subtraction1value
Which operators in the expression share the same precedence level?

Tracing the First Operation

Worked example

Evaluating 5 - 3 - 1

Determine the result of 5 - 3 - 1 using the equal-precedence rule.

  1. 1
    Identify the tie

    Both operators are subtraction operators, so they have equal precedence.

  2. 2
    Evaluate from the left

    The leftmost subtraction is 5 - 3, which produces 2.

  3. 3
    Continue with the remaining expression

    After the first operation, the expression becomes 2 - 1.

  4. 4
    Evaluate the remaining operation

    Compute 2 - 1 to obtain 1.

Result

5 - 3 - 1 equals 1.

5 - 3 - 1equal-precedencesubtraction5 - 3first operation2 - 1remaining expression1final resultread left to right5 - 3 = 22 - 1 = 1
What happens step by step when an expression contains multiple operators with the same precedence?
5 - 3 - 1original expression2 - 1remaining expression5 - 3leftmost operation2result used nextevaluate leftmost subtraction5 - 3 = 2substituted into expression
How does the expression change after evaluating 5 - 3, and what value is used for the next subtraction?

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.

GroupingFirst operationResult
(5 - 3) - 15 - 31
5 - (3 - 1)3 - 13

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.

Worked example

Changing the Order Explicitly

Compare 10 - 5 + 2 with 10 - (5 + 2).

  1. 1
    Evaluate without parentheses

    The operators have equal precedence, so evaluate from left to right: 10 - 5 produces 5, then 5 + 2 produces 7.

  2. 2
    Evaluate with parentheses

    The parentheses force 5 + 2 to be evaluated first, producing 7. Then compute 10 - 7.

Result

10 - 5 + 2 equals 7, while 10 - (5 + 2) equals 3.

10 - 5 + 2left to right7final result10 - (5 + 2)parentheses first3final result10 - 5 = 5; 5 + 2 = 75 + 2 = 7; 10 - 7 = 3
How does adding parentheses change the order of evaluation and the final result?

Mistakes to Avoid

  • Common mistake

    Reading an equal-precedence expression from right to left.

    What it looks like
    Treating 5 - 3 - 1 as 5 - (3 - 1).
    Why it fails

    Equal-precedence operators are evaluated from left to right, not right to left.

    Do this instead

    Evaluate 5 - 3 first, then subtract 1 from the result.

  • Common mistake

    Assuming subtraction can be regrouped without changing the result.

    What it looks like

    Assuming (5 - 3) - 1 and 5 - (3 - 1) are equivalent.

    Why it fails

    Subtraction is not associative, so different grouping produces different results.

    Do this instead

    Keep 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 like
    Evaluating 10 - (5 + 2) as though it were 10 - 5 + 2.
    Why it fails

    Parentheses force their enclosed sub-expression to be evaluated first.

    Do this instead

    Evaluate 5 + 2 first, then subtract the result from 10.

Practice the Trace

EASY

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

  1. 1
    Operators with equal precedence are evaluated from left to right.
  2. 2
    In 5 - 3 - 1, compute 5 - 3 first, then compute 2 - 1, giving 1.
  3. 3
    Subtraction and division are not associative, so changing the grouping can change the result.
  4. 4
    Parentheses override left-to-right evaluation and force a chosen sub-expression to be evaluated first.
  5. 5
    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.

Related concepts

More concepts starting with A