Operator Precedence

Operator precedence determines which operation is performed first in an expression with more than one operator with different precedence. Operators with higher precedence are evaluated before those with lower precedence.

Example of Operator Precedence:

Let’s try to evaluate the following expression,

10 + 20 * 30

The expression contains two operators, + (plus), and * (multiply).The multiplication operator * has higher precedence than so, the first evaluation will be

10 + (20 * 30)

After evaluating the higher precedence operator, the expression is

10 + 600

Now, the + operator will be evaluated.

610

Difference between Operator Precedence and Operator Associativity

In programming, operators are used to perform various operations on data. Understanding how operators interact with each other is crucial for writing correct and efficient code. In this article, we will explore the two most important concepts which are Operator Precedence and Operator Associativity.

Similar Reads

Operator Precedence:

Operator precedence determines which operation is performed first in an expression with more than one operator with different precedence. Operators with higher precedence are evaluated before those with lower precedence....

Operator Associativity:

Operator associativity defines the direction in which operators of the same precedence are evaluated when they appear in an expression. It can be either left-to-right or right-to-left....

Operator Precedence and Associativity Table:

The following tables list the operator precedence from highest to lowest and the associativity for each of the operators:...

Difference between Operator Precedence and Operator Associativity:

Aspect Operator Precedence Operator Associativity Definition Determines the order of evaluation of operators in an expression. Defines the direction of evaluation when operators of the same precedence appear. Priority Higher precedence operators are evaluated before lower precedence operators. Associativity determines the order in which operators are evaluated within an expression of the same precedence. Example In 2 + 3 * 4, * has higher precedence than +, so 3 * 4 is evaluated first. In 2 + 3 + 4, left-associative operators are evaluated from left to right. Representation Precedence levels are often specified in language documentation or expressed with parentheses in expressions. Associativity is usually implicit and is determined by the language specification....