Originally posted by kaffo lekan:
Hello Pawar,
i still dont understand the operation, please break down the explanation futher.
Thanks
kaffo
1.System.out.println(i++ + i++);
2.System.out.println(((i++) + (i++)));
3.System.out.println((i++) + (i++));
initial value of i=1;
Consider step 1:
Let me write i++ + i++ as a + b where a=i++ before the binary + operator and b=i++ after the binary + operator.
Since a post increment is being done,the value of a is 1, value of i is i+1 i.e 2. Now the current value of i is 2, so the value of b is i which is 2 and the value of i is incremented to 3.
So final result after step 1 is:
a=1, b=2, i=3
So the results is printed as (a+b)=(1+2)=3
Write the expression in step 2, again in terms of a and b. Then
the expression would be (a+b).
Again by appliying the same procdeure as above, the value of a is current value of i which is 3 , the value of i after this post increment is 4, the value of b is current value of i, which is 4 and the value of i after this increment is 5.
So the final values are a=3,b=4 and i=5, giing a result of a+b=3+4=7.
Similarly, by applying the same procedure to 3,value of a is current value of i which is 5, i is incremented to 6, value of b is current value of i which is 6, i is incremented to 7.
Final result is a+b=5+6 = 11.
So, the algorithm can be summarised as:
1)Let a=i++ and b=i++
2) The evaluation goes as follows, a=i, i=i+1, b=i, i=i+1,a+b(from left to right).Note that the paranthesis is steps 2, and 3 are redundant and are of no significance in this case!
Hope this helps!