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.
Example of Operator Associativity:
Let’s evaluate the following expression,
100 / 5 % 2
Both / (division) and % (Modulus) operators have the same precedence, so the order of evaluation will be decided by associativity.
The associativity of the multiplicative operators is from Left to Right. So,
(100 / 5) % 2
After evaluation, the expression will be
20 % 2
Now, the % operator will be evaluated.
0
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.
Contact Us