top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Precedence and order of Evaluation in c++

0 votes
268 views

The C++ language follows the standard precedence for the basic arithmetic operators. Precedence rules help in removing ambiguity of the order of operations performed while evaluating an expression.

For example, given an expression like:

2 * 3 / val_1

It becomes difficult for one to assess which arithmetic operation is going to be performed first. We can hence, use the rules pertaining to the precedence of operators to define the manner in which the expression is going to be evaluated. According to precedence of operator both ‘*’ and ‘/’ have the same precedence. Since, the associativity (discussed later) is from left to right the multiplication operation is executed before the division operation. If we want to evaluate the division operation in the expression first and then multiply it with 2, the following code is required:

2 * (3 / val_1 )

We have used the brackets to modify the order of evaluation of the operations in the given expression. Also important for this purpose is the associativity of the operators. Associativity defines the direction in which the expression is evaluated when an operator is involved. The precedence and associativity of all the operators including those introduced by ++ are summarized in table below. The list is in the descending order of precedence.

Operator

Associativity

() [] . à

Left to right

- ! & * ~ ++ --

Right to left

+  -

Left to right

<< >>

Left to right

 < <= > >=

Left to right

= = !=

Left to right

&

Left to right

^

Left to right

|

Left to right

&&

Left to right

||

Left to right

?:

Right to left

= += -= *= /= %= &= |= ^= <<= >>=

Left to right

                                                   Table: Order of Precedence in c++

 

Operators with the highest precedence are performed first. For instance, in the expression

I + u * v;

In the expression the multiplication operation is performed the addition takes place. Parentheses or brackets can be used to override the precedence. For example, in the following expression the addition is performed first.

(I + u) * v;

When operators of the same precedence appear in the same expression, they are usually evaluated from left to right. For example, the following expression illustrates how the associativity law is carried out among the operators with same precedence.

I * u / v;

The expression is evaluated from left to right as the precedence of the operators are same.

Step 1: Multiply I by u

Step 2: Product of I * u divided by v

Example: Precedence of operations in c++:

#include <iostream.h>

#include <conio.h>

void main()

{

    int val_1 = 10, val_2 = 20, val_3 =2, result_ val;

    clrscr();

    result_val = val_1 + val_2 * val_3;

    cout<<”val_1 + val_2 * val_3=” <<result_val;

    cout<<endl;

    result_val =(val_1 + val_2) * val_3;

    cout <<”(val_1 + val_2” * val_3 = “ <<result_val;

    cout<<endl;

}

Output:

Val_1 + val_2 * val_3 = 50

(val_1 + val_2) * val_3 =60

posted Aug 2, 2017 by Alok Kumar

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button

...