-->





Operator Precedence in C++ Programming

What is operator precedence in C++?

  •  “The order in which different types of operators are evaluated is called as operator precedence “. It is also known as hierarchy of operators. All operators in C++ have their own level of precedence. In any expression operators having higher precedence evaluated first.
The Expression is evaluated in following sequence

  1. Arithmetic
  2. Relational
  3. Logical
The Table Shows the Level of Precedence

  • The expression in parenthesis is evaluated first. In case of nested parenthesis the part of expression in the inner most parenthesis is evaluated first.
  • Next, multiplication and division operators are evaluated.
  • Then, plus and minus operators are evaluated
  • Relational operators, Logical AND, OR and finally assignment operator are respectively evaluated.
  • For Example consider the expression 5*(4+ (10-6)/2) +5
  • First of all, the expression in inner most parenthesis (10-6) is evaluated.
  • Second step, result of this expression is divided by 2. It results 2.
  • Third step, 2 is added to 4 the entire values in parenthesis gives result equal to 6.
  • Last step, 6 is multiplied by 5 which are equal to 3o then finally added to 5 and final result is 35.

3 comments:

  1. Nice post. As you probably know, C++ has many more operators, and it can become confusing sometimes. I usually refer to this handy C++ operator precedence table.

    ReplyDelete