【发布时间】:2016-11-02 19:57:06
【问题描述】:
我想要这个输出:
Insert a integer: 13
13
14
16
17
19
使用for 循环,它可以正常工作:
for( ; ; num++)
{
if (num%3==0)
continue;
else
if(num%10==0)
break;
printf("%d\n", num);
}
但是当我尝试更改为 while 循环时:
while(1)
{
if (num%3==0)
continue;
else
if(num%10==0)
break;
printf("%d\n", num);
num++;
}
奇怪的事情发生了:
Insert a integer: 13
13
14
你们能帮帮我吗?
【问题讨论】:
-
在
while循环中,num在num % 3 == 0为真的循环迭代中不会增加。continue将不会跳到while块中的最后一条语句。for循环版本总是 在每次迭代时递增num,因为这是处理for循环中的第三个表达式的方式。
标签: c loops for-loop while-loop