Lesson 22 of 60 – Operator Precedence in C++
37%

Operator Precedence in C++

Operator precedence determines the order in which operators are evaluated in an expression. When an expression contains multiple operators, C++ uses precedence rules to decide which operation is performed first.

Note: Parentheses () can be used to explicitly control the order of evaluation and make expressions easier to understand.

1. What is Operator Precedence?

Operator precedence defines the priority of operators in an expression.

int result = 10 + 5 * 2;

Multiplication has higher precedence than addition, so multiplication is performed first.

5 * 2 = 10
10 + 10 = 20

Therefore, result is 20.

2. Why Operator Precedence is Important

Without understanding precedence, an expression containing several operators can produce a result different from what you expect.

int result = 20 - 5 * 2;

The multiplication is performed first:

5 * 2 = 10
20 - 10 = 10

So the result is 10, not 30.

3. Parentheses Have High Priority

Parentheses can be used to explicitly specify which operation should be performed first.

int result = (10 + 5) * 2;

First:

10 + 5 = 15

Then:

15 * 2 = 30

4. Multiplication Before Addition

int result = 10 + 4 * 3;

Multiplication is performed before addition.

4 * 3 = 12
10 + 12 = 22

The result is 22.

5. Division Before Addition

int result = 20 + 10 / 2;

Division has higher precedence than addition.

10 / 2 = 5
20 + 5 = 25

The result is 25.

6. Multiplication and Division Have the Same Precedence

Multiplication and division have the same precedence. When operators have the same precedence, they are generally evaluated from left to right.

int result = 20 / 5 * 2;

Evaluation:

20 / 5 = 4
4 * 2 = 8

The result is 8.

7. Addition and Subtraction Have the Same Precedence

Addition and subtraction have the same precedence and are generally evaluated from left to right.

int result = 20 - 5 + 3;

Evaluation:

20 - 5 = 15
15 + 3 = 18

8. Left-to-Right Evaluation

When operators at the same precedence level are left-associative, the expression is evaluated from left to right.

int result = 100 / 5 * 2;

Evaluation:

100 / 5 = 20
20 * 2 = 40

9. Right-to-Left Associativity

Some operators are right-associative. Assignment operators are an important example.

int a, b, c;

a = b = c = 10;

The assignments are evaluated from right to left:

c = 10
b = c
a = b

All three variables receive the value 10.

10. Arithmetic Operator Precedence

The common arithmetic precedence order is:

  1. Parentheses
  2. Multiplication, division, and modulus
  3. Addition and subtraction
int result = 10 + 20 * 3 % 4;

The multiplication and modulus operations have higher precedence than addition and are evaluated according to their associativity.

11. Modulus and Multiplication

The modulus operator % has the same precedence level as multiplication and division.

int result = 10 + 17 % 5 * 2;

First, the operators at the higher precedence level are evaluated from left to right:

17 % 5 = 2
2 * 2 = 4
10 + 4 = 14

The result is 14.

12. Relational Operators and Arithmetic Operators

Arithmetic operators generally have higher precedence than relational operators.

bool result = 10 + 5 > 12;

First:

10 + 5 = 15

Then:

15 > 12

The final result is true.

13. Equality Operators

Equality operators such as == and != have lower precedence than relational operators such as > and <.

bool result = 10 < 20 == true;

The relational comparison is evaluated before the equality comparison.

14. Logical AND and OR Precedence

The logical AND operator && has higher precedence than the logical OR operator ||.

bool result = true || false && false;

The AND operation is evaluated first:

false && false = false
true || false = true

15. Using Parentheses with Logical Operators

Parentheses can make the intended logic clear.

bool result =
    (age >= 18) && (hasID == true);

Using parentheses improves readability and reduces mistakes in complex expressions.

16. Assignment Has Lower Precedence

The assignment operator = has lower precedence than most arithmetic, relational, and logical operators.

int result;

result = 10 + 5 * 2;

The arithmetic expression is evaluated first, and the resulting value is then assigned to result.

17. Assignment and Comparison

The assignment operator = should not be confused with the equality operator ==.

int age = 20;

bool result = (age == 20);

The assignment happens when age is initialized, while age == 20 performs a comparison.

18. Unary Operators

Unary operators operate on one operand. Examples include increment, decrement, logical NOT, unary plus, and unary minus.

int x = 5;

int a = -x;
bool b = !false;

Unary operators generally have higher precedence than many binary operators.

19. Increment and Arithmetic

int x = 5;

int result = ++x * 2;

The pre-increment changes x to 6 before it participates in the multiplication.

6 * 2 = 12

20. Conditional Operator Precedence

The conditional operator ?: has lower precedence than many arithmetic, relational, and logical operators.

int age = 20;

std::string result =
    age >= 18 ? "Adult" : "Minor";

The comparison age >= 18 is evaluated before the conditional operator selects the result.

21. Using Parentheses to Change Precedence

int a = 10;
int b = 20;
int c = 5;

int result1 = a + b * c;
int result2 = (a + b) * c;

The two expressions produce different results because parentheses change the order of evaluation.

result1 = 110
result2 = 150

22. A Practical Marks Example

int marks1 = 70;
int marks2 = 80;

double average =
    (marks1 + marks2) / 2.0;

The parentheses ensure that the two marks are added before division.

23. Operator Precedence with User Input

#include <iostream>

int main() {

    int a, b, c;

    std::cout << "Enter three numbers: ";
    std::cin >> a >> b >> c;

    int result = a + b * c;

    std::cout << "Result: " << result;

    return 0;
}

Multiplication is performed before addition in the expression.

24. Reading Complex Expressions Carefully

For complex expressions, break the expression into smaller steps or use parentheses.

int result =
    (10 + 5) * (20 - 15);

First:

10 + 5 = 15
20 - 15 = 5

Then:

15 * 5 = 75

25. Important Precedence Levels

Some commonly encountered precedence levels, from higher to lower, include:

  1. Parentheses and other primary expressions
  2. Unary operators
  3. Multiplication, division, and modulus
  4. Addition and subtraction
  5. Relational comparisons
  6. Equality comparisons
  7. Logical AND
  8. Logical OR
  9. Conditional operator
  10. Assignment operators

This is a simplified learning order. C++ has a more detailed precedence table containing additional operators.

26. Precedence vs Associativity

Precedence determines which operator has priority. Associativity determines the direction in which operators of the same precedence are grouped.

int result = 20 / 5 * 2;

Division and multiplication have the same precedence and are left-associative, so the expression is grouped as:

(20 / 5) * 2

27. Complete Precedence Example

#include <iostream>

int main() {

    int a = 10;
    int b = 20;
    int c = 5;

    int result =
        a + b * c - 10 / 2;

    std::cout << "Result: "
              << result;

    return 0;
}

The multiplication and division operations are performed before addition and subtraction.

b * c = 100
10 / 2 = 5

10 + 100 - 5 = 105

28. Best Practice: Use Parentheses

Even when you know the precedence rules, parentheses can make code clearer.

Less explicit:

int result = a + b * c;

More explicit:

int result = a + (b * c);

Parentheses communicate the intended grouping directly to other programmers and to your future self.

29. Common Operator Precedence Mistakes

  • Assuming addition is performed before multiplication.
  • Confusing precedence with associativity.
  • Forgetting that parentheses can change grouping.
  • Confusing = with ==.
  • Assuming logical OR has higher precedence than logical AND.
  • Writing complicated expressions without parentheses.
  • Assuming all operators have the same precedence.

30. Operator Precedence – Final Summary

Category Examples General Precedence
Parentheses () Very high
Unary operators ++ -- ! - High
Multiplicative * / % Higher than + and -
Additive + - Medium
Relational < > <= >= Lower than arithmetic
Equality == != Lower than relational
Logical AND && Lower than equality
Logical OR || Lower than logical AND
Conditional ?: Lower than logical OR
Assignment = += -= *= /= Low

📌 Key Points

  • Operator precedence determines the priority of operators.
  • Parentheses can explicitly control the order of evaluation.
  • Multiplication, division, and modulus have higher precedence than addition and subtraction.
  • Operators with the same precedence are grouped according to their associativity.
  • Arithmetic operators generally have higher precedence than relational operators.
  • Relational operators generally have higher precedence than equality operators.
  • Logical AND has higher precedence than logical OR.
  • The conditional operator has lower precedence than logical OR.
  • Assignment operators have relatively low precedence.
  • Use parentheses when an expression is complex or when you want the intended grouping to be obvious.

🧠 Quick Quiz

Question: What is the result of the following expression?

int result = 10 + 5 * 2;