“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
when i tried i=i++ with i = 0, even after execution of this statement, value of i is still 0.
The key to this is to understand the difference between the value of the expression and the value of the variable.
what this does is…
1) Get the current value of i. The value of the postfix increment expression is the value of the variable before the new value is stored. So we have to get the “before it’s incremented” value. This is the value of the expression i++.
2) Increment i. This doesn’t alter the value of the expression that was obtained in step 1.
3) Take the value of the expression that was retrieved in step 1 (the value of i before being incremented) and stuff it into i.
in our example… y+=x++… x is incremented before the assignment but as it is post fix notation value of ‘x++’ will evaluate to 5 for any consideration in the expression, otherwise actual value of x is already 6!!!
hence y+=x++ with initial value of y=7 and x=5 will have y=12 and x=6 after its execution.
Y = 12 and X = 6
Why? because X will be incremented after y = Y+X has been carried out and result has been assigned to Y.
Yes Y = 12 and X = 6
if it would have been y+=++x then the value of y would have been equal to 13 and x = 6
I am sure about this
Mohit
It will expand as
y = y + x++ ;
= 7+5
=12
Y will be 12 and x will be 6.
This is bcoz the Associativity of ++ is from Left to Right. So value of x is returned to + and later incremented. Hence the result.
12
y=y+x++
y=5+7stored
so
12answer
x value display 7 but memory stored 8
y+=x++;
y=y+(x++);
x++ is postincrement so
y=7+5=12
After this expression x value will increment because of postincrement.
So x =6.
y=y+x++
here the addition of x and y is performed first and then x is incremented by one
so y=7+5=12
and then x=6
Correct answer :
Y = 12 and X = 6
here in the expression y+=x++; we have two operators, post-fix ‘++’ and ‘+=’ increment & assign.
From the precedenc table, ++ has higher precedenc to += hence evaluated first.
Hence,
after executing y+=x++ we will have x=6 and y=13
when i tried i=i++ with i = 0, even after execution of this statement, value of i is still 0.
The key to this is to understand the difference between the value of the expression and the value of the variable.
what this does is…
1) Get the current value of i. The value of the postfix increment expression is the value of the variable before the new value is stored. So we have to get the “before it’s incremented” value. This is the value of the expression i++.
2) Increment i. This doesn’t alter the value of the expression that was obtained in step 1.
3) Take the value of the expression that was retrieved in step 1 (the value of i before being incremented) and stuff it into i.
in our example… y+=x++… x is incremented before the assignment but as it is post fix notation value of ‘x++’ will evaluate to 5 for any consideration in the expression, otherwise actual value of x is already 6!!!
hence y+=x++ with initial value of y=7 and x=5 will have y=12 and x=6 after its execution.
y=12 and x=6
because y+=x++ means y=y+x.
here y=7 & x=5(will not change)
so y=7+5=12
&
x=6(increase after )
x=6 & y=12 post increment
In statements like y+=x++, the evalauiton looks like below.
1. it is post increment so the value of x is first assigned to y and then incremented. y+=5, y=y+5
at last y is 12
2. x++ is incremented now, x=6
x=6
y=12
Leave an Answer/Comment