Expressions, Order Of Expressions - Operator Precedence & Associativity

Expressions, Order Of Expressions - Operator Precedence & Associativity

 Hi guys, in this section we will discuss how Operator Precedence & Associativity works and how the calculations are computed. So without wasting time let's get started.

Let's have a simple example:

    int x = 5 - 17 * 6   (This is an expression)

The expression means lines of code or command in any language as shown above example.

Precedence:

The operator's precedence determines which operation is performed first in an expression with more than one operator in the expression.

Let's understand some examples:

   Expression: 10 + 20 * 30
The above expression is evaluated as: 
(10 + 20) * 30 = 900 and also can be done as
 10 + (20 * 30) = 610

Without any rules or precedence, there are two types of answers in programming and coding language. So, in C Programming we standardize it with some rules and preference which is known as precedence and associativity.

Note: Don't apply the BODMAS rule in computer calculations.

Operator Precedence Table:

Here we discuss the basic of operator precedence for our common uses.

Here operators of higher priority are evaluated first in the absence of parenthesis "( )". For more understanding, you can follow the big table.


Operator Associativity:

When operators of similar priority are present in an expression, then the tie is taken care of by Operator Associativity, and the value of the expression is evaluated.

Let's take a basic example:

b * a / z  - refers to  - (b*a) / z

b / a * z -  refers to - (b / a) * z

Note: *, / follows the left-to-right associativity rule. Follow the big table for more advanced knowledge.


That's All For This Section. For More Understanding & Explanation Watch Videos On YouTube.

Watch On YouTube😎






 

Post a Comment