What is the difference between these two for loops in C?
This question already has an answer here:
As for the first one, i
is decremented before the loop body is executed. The second one decrements i
after the loop body is executed.
The difference is the step in which i
is actually decremented, which affects the values of i
seen inside the loop body.
The second traditional version decrements i
after the loop body is executed, and before the condition is checked again. Thus i
reaches 0 after the loop body is executed for i == 1
. The condition is checked again and after the loop i
is 0.
The first version decrements i
before the loop body is executed, as part of the condition being checked. Here the loop body runs the first time with i == n - 1
and the last time with i == 0
. Then i
is decremented and its previous value compared against 0
. The loop exits and i
is -1 after it.
In the traditional version, the loop body always sees the same value against which the conditional part was checked.
链接地址: http://www.djcxy.com/p/1986.html上一篇: 什么运营商`<
下一篇: 这两个for循环在C中有什么区别?