Operators Precedence

When various operators are used within the same statement a problem arises which is which operation should be performed first. To solve this problem there is the concept of operator precedence. Operator Precedence is a set of rules that defines the priority for all the operators present in a programming language. The operation which has an operator with the highest priority will be executed first.

Example:

a= 4+5/10;
In the above given example if we solve the addition first then the answer will be 9/10=0.9 .
Now if we solve the division first then the answer will be 4 + 0.5 = 4.5 .
Out of this the second answer is a valid answer because the precedence of division is more than addition.
Therefore a=4.5

Operator Precedence and Associativity in Programming

We have many types of operators in a programming language. Operators serve many purposes. But when we have many operators within the same statement, Operator Precedence and Associativity come into the picture. They define which operator should be taken into account first. It is similar to the BODMAS rule we have learned in our school mathematics. In this article, we will see what is Operator Precedence and Associativity.

Similar Reads

Operators Precedence:

When various operators are used within the same statement a problem arises which is which operation should be performed first. To solve this problem there is the concept of operator precedence. Operator Precedence is a set of rules that defines the priority for all the operators present in a programming language. The operation which has an operator with the highest priority will be executed first....

Operator Associativity:

If we have operators which have same precedence in the same statement then we use associativity to figure out which operation should be performed first. Associativity defines whether you should go from left to right or you should go to right to left. If Associativity says that you should go left to right then operation on the left side should be performed first and then only we should move to right side....

Operator Precedence and Associativity table:

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

Contact Us