Here is why:
You have a memory location a which holds an int.
A side-effect of both expressions a++ and ++a is that the value of a is incremented.
However, the value of expression a++ is the value a had before increment, whereas the value of ++a is the value it has after increment.
So, in the first example where a is originally 10, we have:
* a++ evaluates to 10, but increments a to 11
* subsequent +a evaluated to 11+1 = 12
In total 10 + 12 = 22
Similar for loops i =1,2,3.
Hope this helps,
Panagiotis.
Originally posted by Aftab Ahmed:
Hi,
can anybody tell about this
int a=10;
for(int i=0,i<4,i++)
System.out.println(a++ + ++a);
why v get that ans's
22
26
30
34
thanx
aftab