Dr. Starke has a great post of what happens if you try i = 1; i += ++i + ++i; in different languages.
It is definitely worth reading if you are a programmer.
It is also quite amusing.
One thing that puzzles me, though, is that he says 8 is 'a bit off' as a result (he even goes on to poke some fun at PHP: "Eight again. So many commercial websites are build with php - and they all calculate like that? re-consider your e-commerce behaviour..."
Well, let's give it a thought:
i = 1;
i += ++i + ++i;
Depending on the language semantics and implementation, you end up with different results.
PHP logic obviously goes like this:
1. Calculate the right side of the expression;
2. Add whatever the result from step 1. is to the value stored in i;
So initially, i is 1. The first ++i makes it 2. Ah, 2, PHP says, let's remember that. Then the second ++i increments i to 3. PHP then adds 2 to 3 and then adds the result to the current value of i (which is 3). So here we go:
i += 2 + 3 => 8
So eight seems like a legit answer to me.
What about the other answers?
Let's start with C / Objectvie-C first. The result produced by C is 9 which seems quite puzzling at first. But consider that C is very low-level and close to the metal language. While the general logic stays the same as we outlined with PHP, C implementation obviously has a twist. And the twist is that the second ++i will also update the result from the first one as it works over the same memory location.
You see, when PHP calculates the first ++i, it updates the value in i to store 2 and then remembers the outcome (also 2) in a separate variable. Thus the second increment of i has no effect over it.
C in contrast remembers not the outcome itself, but the address in the memory, where the outcome is stored. And as the memory location is one and the same for the two ++ operations, C ends up performing 3+3 and then adds the result to the contents of the same memory address (again 3).
As for C#, Java and JS, which all end up with 6, it seems that they simply translate the i +=... operator to i = i +... And so they end up with an expression like i = i + ++i + ++i which translates to i = 1 + 2 + 3.
The bottom line, of course is that it is amusing and somewhat bizarre (scary?) how differently languages can behave in situations when you expect them all to do the same thing. But as one commenter puts it: Whoever writes such code deserves whatever answer they get.
True words.
Thanks for the link, @softmodeling
Update: Note that most of the above explanations actually describe the behaviour of the compiler. For instance, when I write 'C in contrast remembers not the outcome itself, but the...' it is actually the compiler (gcc) that does that.