expression evaluation based on precedence – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Exam Questions-ICSE2018-03B #JAVA#4053    siteicon   siteicon  

Problem Statement - expression evaluation based on precedence

What is the value of y after evaluating the expression given below?
y+=++y + y– + –y;  when int y=8

Solution

TC++ #4053

If int y = 8 we will put the value in expression and evaluate

y +=  ++y + y– + –y;
+= being the composite assignment, will be evaluated in the end so step 1 would to evaluate RHS
Step 1:  RHS->  9 + 9 + 7;  //First y will pre-increment, second will remain 9, third will decrement twice
Step 2: y = y+25;  //Composite operator expanded, original y will remain as it is while expanding. This is because y will be filled first but due to last precedence of assignment it will not be impacted.
Step 3: y = 8+25 ; //Original value of y will be used here

So final y  = 33

 

 


Share

sunmitra| Created: 25-Mar-2018 | Updated: 12-Apr-2019|ICSE2018






Back