It's down to the C++ specification, which is based on the older C specification, which does not state exactly when pre and post increment instructions must be performed - or indeed that the order of execution of instructions must be maintained to be the same as you wrote: the compiler is free to reorder them to suit it's execution pipeline if it wishes.
This means that what you get when you try to play games with pre-and post increment is not defined by the specification and varies from compiler to compiler.
This is not the case in C#, where the execution order is very specifically defined, and the exact timing of pre and post increments are also specified.
i = 10;
x = ++i + 5;
Can be read as:
i = 10;
i = i + 1;
x = i + 5;
Is that it? It's a bit...simple...
Yes, it is. Or, perhaps not.
It is simple, if you use it in simple ways - as an array indexer for example:
x = myArray[index++];
Or as a loop increment:
for (i = 0; i < 10; i++)
{
WriteLine(myArray[i]);
}
But after that, you are into a world of confusion and pain!
For example, what does this leave as a value of i:
int i,j;
i = 10;
for (j = 0; j < 5; j++)
{
i = i++;
}
The answer is: unchanged. i remains at 10. Why? Think of it like this: what does this look like if we expand it?
int i = 10;
i = i++;
If we write this in C#, then the IL looks like this:
.line 14,14 : 13,24 ''
IL_0001: ldc.i4.s 10 Push a constant value '10' to the stack, 4 byte integer,
IL_0003: stloc.0 Pop the top of the stack into local number 0
.line 15,15 : 13,21 ''
IL_0004: ldloc.0 Push local number 0 to the stack
IL_0005: dup Duplicate the top of the stack
IL_0006: ldc.i4.1 Push a constant value '1', 4 byte integer
IL_0007: add Pop the top two stack items, add them, and push the result
IL_0008: stloc.0 Store the top of the stack in local number 0
IL_0009: stloc.0 Store the top of the stack in local number 0
What? In expanded C# code, that comes back as:
int i = 10;
int j = i;
int k = j;
k = k + 1;
i = k;
i = j;
Which is rather strange...because you could throw away the three lines in the middle without affecting the results.
It shouldn't do that, should it?
Yes. Yes, it should: i++ is a postfix operation: It says, "remember the value of i, then increment i by one, and then return the value you remembered".
So what you have told the compiler to do is ignore the result of the increment by overwriting it with the value you started off with.
Interestingly, if you try it in the Visual Studio C++ compiler...it doesn't... because it handles it differently!
So, now we have the first reason why you have to be careful when you start using increment and decrement operators for real: it's effectively a side effect, a whole line of code inserted into your line, and if you don't think very carefully, it won't do what you think.