top of page

Operator Precedence

What is it?

  • Precedence and Associativity are two characteristics of operators that determine the evaluation order of subexpressions in absence of brackets.

Characteristics

of Operator

Precedence

Associativity

Precedence

  • Precedence determines which operator is performed first in an expression with more than one operators with different precedence.

  • Within an expression, higher precedence operators will be evaluated first.

  • For eg.

Evaluate 10 + 20 * 30 

10 + (20 * 30)

​

(10 + 20) * 30

Multiplication is granted a higher precedence than addition.

Associativity

  • Associativity is only used when there are two or more operators of the same precedence in an expression.

  • All operators with the same precedence have the same associativity.

  • Precedence and associativity of postfix ++ and prefix ++ are different.

  • For eg.

Evaluate 100 / 10 * 10

(100 / 10) * 10

* and / have the same precedence and their associativity is Left to Right.

Associativity can be either Left to Right or Right to Left.

Operator Precedence in Programming

  • Here, operators with the highest precedence appear at the top of the table.

Why Precedence?

  • Specifying the order of an expression is evaluated with a different operator.

  • For a programmer, It is good to know precedence and associativity rules.

  • But the best thing is to use brackets.

  • Brackets increase readability of the code.

bottom of page