我只是想知道为什么
int i;
for (i=0; i<5; i++){
printf("%d\n",i)
}
printf("Here i get the result that misleads me : %d\n",i)最后一个值是5。
我的逻辑是:
From 0 to 4 -> printf
If i > 4 (since we are dealing with integers) stop the loop.但是循环停止在4,而不是5!为什么我在循环结束后得到5?为什么它会增加呢?
武断?
谢谢,
发布于 2013-11-30 15:41:10
让我们看看这个循环:
for (i = 0; 0; i++)
do_smth();您将看到这个循环根本不执行。因为条件从一开始就是假的。
现在
for (i = 0; i < 4; i++)
i++;显然,每次运行都会增加两次i (一次在循环中,然后在,然后是,因为for的第三部分)。最后,i将具有第一个偶数值,从而使条件为假。那是6。因此,在步骤和循环外部变量的值之间没有链接。
https://stackoverflow.com/questions/20302035
复制相似问题