I just wanted to create a little Java-Puzzle, but I puzzled myself. One part of the puzzle is:
What does the following piece of code do:
i += ++i + i++ + ++i
;
i=1
at starti += X
-> i = i + X
-> i = 1 + X
(so lets count X)++i
will be incremented to 2 and return 2
i++
will return 2
and then be incremented to 3++i
will be incremented from 3 to 4 and return 4
X = 2 + 2 + 4 = 8
So i = 1 + 8
-> i=9
You would get 12 if your code would be something like this
int i = 1;
int tmp = ++i + i++ + ++i;
i += tmp;
because then your code would be i=1
, and after calculating tmp i would be i=4
, then i+=tmp
-> i=4+8=12