我在 C 中有一个非常简单的问题。我正在尝试编写一个简单的程序,输出 10
和 100
之间的 10
的倍数, inclusive(即:在闭区间 [10,100]
上)跳过 30
和 70
并垂直输出值。
这是我的代码:
#include<stdio.h>
main()
{
int i=10;
do {
if(i==30||i==70)
continue;
printf("\n %d",i);
i++;
} while(i<100);
return 0;
}
程序在 29
处停止,跳过 30
并继续进入永无止境的循环。怎么了?
最佳答案
问题是,当您点击 if
语句时,您现在正在跳过 i
的增量。所以你永远达不到 100!
#include<stdio.h>
main()
{
int i=10;
do {
if(i==30||i==70)
continue; //!!!! This will skip the i increment
printf("\n %d",i);
i++;
} while(i<100);
return 0;
}
我推荐一个 for
循环:
main()
{
for (i = 10; i < 100; i++) {
if(i==30||i==70)
continue; // The for loop will do the i++ on the "continue"
printf("\n %d",i);
}
return 0;
}
关于c - a c 迭代,同时在 do while 循环中跳过给定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21030780/