Concepts / Operator Precedence and PEMDAS

Operator Precedence and PEMDAS

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

  • Programming

The Tie in the Middle

An expression can contain several operators with equal precedence. When that happens, precedence alone does not decide which operator comes first. The tie is broken by evaluating from left to right. This rule is especially important for subtraction and division, because changing their grouping can change the result.

What do you think happens?

What result should you expect from 5 - 3 - 1?

  • 1
  • 3
Reveal answer

Answer: 1

The subtraction operators have equal precedence, so evaluation proceeds from left to right: first 5 - 3 produces 2, then 2 - 1 produces 1.

Reading Equal-Precedence Operators

Operator precedence describes which operations are considered first when an expression contains different operators. When two or more operators share the same precedence level, the left-to-right rule decides the order. The computer processes the operator that appears farther left before the equal-precedence operator to its right.

evaluate leftmost operatorreplace 5 - 3 with 2evaluate remaining subtraction5 - 3 - 1two equal-precedenceoperators5 - 3leftmost subtraction2 - 1remaining expression1final result
What happens next when an expression contains two subtraction operators with the same precedence?

Left-to-right evaluation breaks a precedence tie. It does not mean that every operation in every expression is always performed from left to right; it applies here when the operators being compared have equal precedence.

Tracing 5 - 3 - 1

Evaluating the expression step by step

Evaluate 5 - 3 - 1 using the left-to-right rule.

Step 1: Both subtraction operators have equal precedence. Start with the leftmost subtraction, 5 - 3, which produces 2.

Step 2: Replace the completed part of the expression with its result. The remaining expression is 2 - 1.

Step 3: Evaluate the remaining subtraction: 2 - 1 produces 1.

5 - 3 - 1 equals 1.

evaluate 5 - 3evaluate 2 - 15 - 3 - 1initial expression2 - 15 - 3 = 212 - 1 = 1
How does the value change after evaluating 5 - 3 and then subtracting 1, instead of evaluating 3 - 1 first?

Why Grouping Changes the Answer

Subtraction is not associative. That means changing the grouping can change the result. The left-to-right grouping of 5 - 3 - 1 is (5 - 3) - 1, which equals 1. A different grouping, 5 - (3 - 1), equals 3. These are different expressions because the parentheses tell the computer to evaluate a different sub-expression first.

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

Addition and multiplication are associative in the source examples: (a + b) + c equals a + (b + c), and (a * b) * c equals a * (b * c). Subtraction and division are not associative, so their grouping requires particular care.

Parentheses as an Override

Parentheses explicitly group a sub-expression and force that sub-expression to be evaluated first, even when it appears later in the expression. Use them when the intended grouping differs from the normal left-to-right order or when you want the grouping to be unmistakable.

evaluate left to rightevaluate parentheses first10 - 5 + 2left-to-right grouping10 - (5 + 2)parenthesized grouping710 - 5 = 5; 5 + 2 = 735 + 2 = 7; 10 - 7 = 3
How does adding parentheses change the order of operations and the final result?

Controlling the grouping

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

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

Expression with parentheses: The parentheses force 5 + 2 to be evaluated first, producing 7. Then evaluate 10 - 7, which produces 3.

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

Add parentheses when you need a grouping other than the default left-to-right grouping. This makes the intended evaluation order explicit and helps prevent calculation errors.

Mistakes with Equal Operators

  • Evaluating the rightmost subtraction first in 5 - 3 - 1.

    The subtraction operators have equal precedence, so the tie is resolved from left to right.

    Fix: Evaluate 5 - 3 first, then evaluate the remaining expression 2 - 1.

  • Assuming that equal-precedence operators can be grouped in either direction.

    Subtraction is not associative, so different groupings can produce different results.

    Fix: Use the left-to-right rule unless parentheses explicitly request a different grouping.

  • Ignoring parentheses when determining the first operation.

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

    Fix: Evaluate 5 + 2 first, then subtract that result from 10.

Check Your Reasoning

EASY

Evaluate 10 - 5 + 2 without parentheses. Then evaluate 10 - (5 + 2). State which operation is performed first in each expression and explain why the results differ.

Hints
  • For the expression without parentheses, look for equal-precedence operators and apply the left-to-right rule.
  • For the expression with parentheses, evaluate the enclosed sub-expression first.
EASY

Explain why 5 - 3 - 1 produces 1 rather than 3. Write the two intermediate expressions created during left-to-right evaluation.

Hints
  • Begin with the leftmost subtraction.
  • After evaluating 5 - 3, write the remaining expression before calculating the final result.

The Rule to Remember

  1. When operators have equal precedence, evaluate them from left to right.
  2. The expression 5 - 3 - 1 equals 1 because 5 - 3 is evaluated before the remaining subtraction.
  3. Subtraction and division are not associative, so changing their grouping can change the result.
  4. Parentheses override the normal left-to-right grouping and force a chosen sub-expression to be evaluated first.
  5. Use parentheses when you need to control or clarify the evaluation order.

Key Takeaways

  • Equal-precedence operators are evaluated from left to right.
  • 5 - 3 - 1 is evaluated as (5 - 3) - 1, producing 1.
  • Subtraction and division require careful grouping because they are not associative.
  • Parentheses force a sub-expression to be evaluated first and can change the final result.