Is there a difference in ++i
and i++
in a for
loop? Is it simply a syntax thing?
The question is:
Is there a difference in ++i and i++ in a for loop?
The answer is: No.
Why does each and every other answer have to go into detailed explanations about pre and post incrementing when this is not even asked?
This for-loop:
for (int i = 0; // Initialization
i < 5; // Condition
i++) // Increment
{
Output(i);
}
Would translate to this code without using loops:
int i = 0; // Initialization
loopStart:
if (i < 5) // Condition
{
Output(i);
i++ or ++i; // Increment
goto loopStart;
}
Now does it matter if you put i++
or ++i
as increment here? No it does not as the return value of the increment operation is insignificant. i
will be incremented AFTER the code's execution that is inside the for loop body.