There is a big difference between postfix and prefix versions of ++.
In the prefix version (i.e., ++i), the value of i is incremented, and the value of the expression is the new value of i.
In the postfix version (i.e., i++), the value of i is incremented, but the value of the expression is the original value of i.
Let’s analyze the following code line by line:
int i = 10; // (1)
int j = ++i; // (2)
int k = i++; // (3)
iis set to10(easy).- Two things on this line:
iis incremented to11.- The new value of
iis copied intoj. Sojnow equals11.
- Two things on this line as well:
iis incremented to12.- The original value of
i(which is11) is copied intok. Soknow equals11.
So after running the code, i will be 12 but both j and k will be 11.
The same stuff holds for postfix and prefix versions of --.