I have a question. How come when I have a for statement and in the middle section, I increment a variable, the variable gets incremented before I go into the for loop? For example,
Code:
for (i = 0; i < 10; ++i)
printf("%d ", i);
printf("\n");
for (i = 0; i++ < 10;)
printf("%d ", i);
printf("\n");
The first for loop is a much more common example, and will print 0 through 9. After each time through the loop, the variable i gets incremented by one, as expected.
The second one puzzles me a bit though. I understand we don't need all three parameters with the for statement, however, when we execute it, it prints 1 - 10. i is getting incremented before we execute the body of the loop...
I believe I understand why the loop prints 10 and doesn't exit before then. Correct me if I'm wrong, but the < operator has higher precedence over the ++ operator, right? So first we check if i is greater than 10 and then we increment i, hence the reason we get 10 printed. I just don't understand why i gets incremented before the body gets executed. Is this the way it always is, or is this perhaps compiler specific? For example, with some compilers, it might get incremented after the body gets executed.