Tuesday 14 July 2015

Operator Precedence :


The order of precedence in C++ language is as follows :

  • Any expression given in parentheses is evaluated first.
  • Then multiplication *  and division / operators are evaluated.
  • Then plus +  and minus - operators are evaluated.
  • In case of parentheses within parentheses, the expression of the inner parentheses will be evaluated first.


Example :

            The expression 5 * (4 / (5 - 1 ) ) + 3 is evaluated in the following order :
  1. First of all, the expression 5 - 1 will be evaluated. It gives a value 4.
  2. Secondly, 4 will be divided by the result of last line i.e. 4 / 4 giving value 1.
  3. Thirdly, will be multiplied by i.e. giving a result 5.
  4. Finally, 5 will be added in 3 and the last result will be 8.

No comments:

Post a Comment