Introduction to Operator Precedence and Associativity in C++

 Operators Precedence &  Associativity






Operators Precedence in C++:

If there are many operators in only one expression, the operators are not assessed simultaneously. Rather, operators with higher precedence have their operator evaluated first.

Let's see following Example:

int a = 50 - 7 * 62;

Here, the multiplication operator ( * )  is of more significant level precedence than the deduction operator - . Consequently, 7 * 62 is assessed first.

Accordingly, the above expression is comparable to

int a = 50 - ( 7 * 6 ) ;

If we want to assess 5 - 7 first, we should wall them in inside brackets :

int a = (5 - 17) * 6;



Code in C++

#include <iostream>
using namespace std ;
int main( ) {
  int no1 = 50 - 17 * 62 ;
  int no2 = 50 - (17 * 62) ;
  int no3 = ( 50 - 17 ) * 62 ;
  cout << " 1st Number = " << no1 << endl ;
  cout << " 2nd Number = " << no2 << endl ;
  cout << " 3rd Number = " << no3 << endl ;
  return 0 ;
}

Output

 1st Number =2046
 2nd Number =-1004
 3rd Number =2046


Operators Associativity in C++:

Operator associativity is the direction from which an expressed is assessed. For instance,

int x = 10 ;

int y = 40 ;

// x will be 40

 x = y ;

Investigate x = 40; statement. The associativity of the ( = ) is from right to left. Thus, the value of y is assigned to x, and not in the other distance.


Operators          Operations               Precedence(Order of evaluation)


(  )                     Parentheses                Assessed first. Assuming the brackets are nested,                                                                    the expression in the deepest pair is assessed first


* ,  / ,  %             Multiplication ,         Assessed second. Assuming there are a few, they're                            Division & Modulus    assessed passed on to right.

        


+ , -                    Add & Subtract         Assessed last. Assuming there are a few, they're                                                                       assessed passed on to right.


Additionally, various operators can have a similar degree of precedence ( as we can see from the above table ). At the point when various operators of a similar precedence level are utilized in an operation, they are assessed by their associativity.

int x = 1 ;
int y = 4 ;
y += x - = 6 ;

The two operators += and - = operators have a similar priority. Since the associativity of these operators is from right to left, this is the way the last sstatement is assessed.

x - = 6 is assessed first. Consequently, a will be - 5

Then, at that point, y += - 5 will be assessed. Consequently, b will be - 1



Example : Operators Associativity


Code in C++

#include <iostream>
using namespace std ;
int main( ) {
  int x = 1;
  int y = 4;
  y += x - = 6;
  cout << " x = " << x << endl; 
  cout << " y = " << y;
}


Output

 x =-5
 y =-1


I hope You can easily Understand and Learn this Article

Best of Luck 😇

Post a Comment

0 Comments