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 :
- First of all, the expression 5 - 1 will be evaluated. It gives a value 4.
- Secondly, 4 will be divided by the result of last line i.e. 4 / 4 giving value 1.
- Thirdly, 5 will be multiplied by 1 i.e. giving a result 5.
- Finally, 5 will be added in 3 and the last result will be 8.
No comments:
Post a Comment