C++ PROGRAMMING ยท 14 MIN STUDY

Operator precedence and associativity

Predict how C++ groups an expression, use parentheses to make intent obvious, and avoid relying on tricky evaluation order.

Learning objectives

  • Explain precedence and associativity.
  • Group arithmetic, comparison, and logical expressions correctly.
  • Use parentheses to communicate intent.
  • Separate grouping rules from operand evaluation order.

Prerequisites: arithmetic, comparison, logical, and assignment expressions. Review Expressions and arithmetic and Operators first.

What precedence means

Precedence determines how operators group when parentheses are absent. Multiplication has higher precedence than addition, so 2 + 3 * 4 groups as 2 + (3 * 4), giving 14 rather than 20.

C++17
#include <iostream>

int main() {
    int result = 2 + 3 * 4;
    int parenthesized = (2 + 3) * 4;
    int chained = 20 - 5 - 3;
    int left = 0;
    int right = 0;
    left = right = 7;

    std::cout << result << '\n';
    std::cout << parenthesized << '\n';
    std::cout << chained << '\n';
    std::cout << left << ' ' << right << '\n';
}

Expected output: 14, 20, 12, then 7 7. The first result groups multiplication before addition; the second changes the grouping with parentheses; the third groups subtraction left-to-right; the final line demonstrates right-associative assignment.

Parentheses can explicitly request a different grouping: (2 + 3) * 4 gives 20.

Useful precedence relationships

This is a learning summary, not the complete precedence table. When in doubt, consult a reference or add parentheses.

OperatorsRelative relationshipExample grouping
Unary ! ++ -- + -tighter than multiplication!ready
* / %tighter than + -a + b * c
+ -tighter than comparisonsa + b < limit
< <= > >=tighter than equalitya < b == ready
== !=tighter than logical ANDa == b && ready
&&tighter than logical ORa && b || c
Assignment = +=low precedencex = a + b

Associativity

Associativity tells how operators of the same precedence group. Addition is left-associative: 20 - 5 - 3 groups as (20 - 5) - 3, giving 12. Assignment is right-associative: a = b = 7 groups as a = (b = 7). Avoid long chained assignments when separate statements are clearer.

Parentheses make intent visible

bool eligible = (age >= 18) && (has_id || has_guardian);
int average = (first + second + third) / 3;

Parentheses are not a sign of weak programming. They help humans review conditions and calculations, especially when several operator groups are mixed.

Precedence is not evaluation order

Precedence describes grouping; it does not mean every operand is evaluated from left to right. C++ has separate sequencing rules. Avoid expressions that modify the same variable multiple times without a clear sequencing relationship. Prefer one meaningful update per statement.

// Clear and easy to reason about:
int value{4};
int before = value;
++value;
int after = value;

Common mistakes

  • Assuming addition happens before multiplication because it appears first in the source.
  • Reading a chain such as a < b == ready without applying the relational-before-equality rule.
  • Assuming left associativity means operands are always evaluated left-to-right; grouping and sequencing are separate rules.
  • Writing a dense condition without parentheses when the intended groups are not obvious.

Where these rules matter

Precedence and associativity affect everyday calculations, validation conditions, Boolean filters, and compound expressions. In reviewed or safety-sensitive code, explicit parentheses make the intended grouping easier to verify. Parentheses clarify grouping; they do not change the language's sequencing rules.

Practice exercises

  1. Evaluate 5 + 2 * 6.
  2. Evaluate 18 / 3 * 2. Explain associativity.
  3. Add parentheses to show the grouping of a < b && c == d || ready.
  4. Write a condition that checks that score is from 40 through 100 inclusive and submitted is true.
  5. Explain why precedence alone does not guarantee left-to-right evaluation of every operand.

Solutions

Show exercise solutions
  1. 5 + (2 * 6) = 17.
  2. (18 / 3) * 2 = 12. Multiplication and division share precedence and group left-to-right.
  3. ((a < b) && (c == d)) || ready.
  4. (score >= 40) && (score <= 100) && submitted.
  5. Precedence decides which operators bind together. Sequencing rules decide when operands are evaluated. They are different language rules.

Summary

Precedence determines grouping across different operator levels; associativity handles operators at the same level. Use parentheses for readability. Do not confuse grouping with evaluation order.

References