Detailed Print – CSKC
Sign In

Search

siteicon
Code Learning#CPP#1752 Back

Simple case of precedence

The order in which operators are applied on operands in an expression can be understood with this example.

Learning Objectives

  • Learning steps in simple precedence like the arithmetic precedence learnt at school level.

Source Code

		 

Click to open popup

Run Output

15

Code Understanding

int x=12 – 5 + 12 / 3 * 2;
In general the BODMAS (Bracket, Of, Divide, Multiply, Add, Subtract) precedence as learned in schools apply. But since c++ has a long list of operators, precedence confusions may arise. For time being let us try to solve above with simple rules only.
12-5+12/3*2
12-5+4*2  (Divide applied)
12-5+8 (Multiply applied)
7+8 =15 (Solved left to right when only add subtract are left)
So the output would be 15.

Notes


Suggested Filename(s): precedence.cpp, simpleprecedence.cpp