Precedence and Associativity

In this lesson, we will discuss the precedence and associativity of arithmetic operators.

Precedence

In case there is more than one operator in an expression, precedence determines the order in which the operators should be evaluated. The operator with higher precedence will be evaluated first in an expression. For example, multiplication * has higher precedence than addition +. Therefore, we first evaluate multiplication in an expression.

Note: In case of parenthesis (), we first evaluate the expression inside the parenthesis.

Example program

Let’s write a program that will evaluate an expression according to its operator precedence.

Run the code below and see the output!

Get hands-on with 1200+ tech skills courses.