If i = 0, why is i += i++ 0?
stackoverflow.com
stackoverflow.com
http://stackoverflow.com/questions/24853/what-is-the-differe...
"++i will increment the value of i, and then return the incremented value."
"i++ will increment the value of i, but return the pre-incremented value."
i += i++;
Console.write(i);
could print 0, 1, or 2 depending on the order in which i++
and i += <value>
is evaluated. From the winning answer, what's really happening is: int i = 0;
i = i + i;
i + 1; // Note that you are discarding the calculation result
This kind of nonsense is the reason why Python doesn't have prefix and postfix increments.int main() { int i=0; return i += i++; }
Returns 0, as expected.
(0) + (0)++ = 0 next time around (1) + (1)++ = 2
If you run
for (int x = 0; x < 10; x++)
{
i += i++;
Console.WriteLine(i);
}
i is always 0.