C++ Operator Precedence
The order in which different type of operators in an
expression are evaluated is known as operator precedence. C++ operator
precedence is also known as hierarchy of operators.
Each operator has its own precedence level. If an
expression contains different types of operators, the operators with higher
precedence are evaluated before the operators with lower 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 plush + and minus – operators are evaluated.
- In case of parentheses within parentheses, the expression of the inner parentheses will be evaluated first.
Example:
The expression 10*(24/(5-2))+13 is evaluated in the
following order:
- First of all the expression 5-2 will be evaluated. It gives a value 3.
- Secondly, 24 will be divided by the result of last line i.e. 24/3 giving value 8.
- Thirdly, 10 will be multiplied by 8. i.e. giving result 80.
- Finally 80 wil be added in 13 and the last result will be 93.
10*(24/5-2)+13
10*(24/3)+13
10*8+13
80+13
93
Post a Comment