#include <stdio.h>
int main()
{
int i; // i wanted to print numbers each with difference of 2 up to 18
for (i = 0; i < 25; i++)
{
printf("the number we get is %d\n\n", i);
++i;
if (i == 18)
{
break;
}
}
return 0;
}
我删除了++i;在 if 语句之前它起作用了。但我不明白为什么它不能与 ++i 一起工作。使用 ++i 它打印最多 24 个,差异为 2(我得到的是由于 i++ & ++i 使用)
i++
语句中的for
在每次循环后执行。
所以i
在if
中永远不会是18岁。
调试:
您使用
i
初始化 0
并在每次迭代中将其递增两次。关键部分是你有终止条件的地方:
printf("the number we get is %d\n\n", i); // prints 18
++i;
if (i == 18) { // i will always be 19 here
这显然错过了终止条件。