For loop why the variable out of the loop is "last index" + step?

c, for-loop, increment

Solution

There are three clauses in the for statement.

- The init-stmt statement is done before the loop is started, usually to initialize an iteration variable.

- The condition expression is tested before each time the loop is done. The loop isn't executed if the boolean expression is false (the same as the while loop).

- The next-stmt statement is done after the body is executed. It typically increments an iteration variable.

So, end of each for loop execution, increment operation executed, and, in the 4th iteration, value of i is 5 and the for loop was broke as the value is 5 in 5th iteration.

Problem

I was just wondering why this : ``` int i; for (i=0; i<5; i++){ printf("%d\n",i) } printf("Here i get the result that misleads me : %d\n",i) ``` The last value is 5. My logic is : ``` From 0 to 4 -> printf If i > 4 (since we are dealing with integers) stop the loop. ``` But the loop stopped at 4 not 5 ! Why do I get 5 after the loop ? Why does it ever increment ? Arbitrary ? Thanks,

Original source