c++中continue的问题

来源:百度知道 编辑:UC知道 时间:2024/04/29 05:27:51
#include<iostream.h>
void main()
{
for(int n=100;n<=200;n++)
if(n%3==0)
continue;
cout <<n <<endl;
}

201
Press any key to continue
为什么会得出201呢,不明白。一点都不明白。咋会事了。

continue 这个转向语句的作用是跳出本次循环,你在for语句后面没有加上{ }限定for循环区间,所以每次都会执行continue

#include<iostream.h>
void main()
{
for(int n=100;n<=200;n++)
{if(n%3==0)
continue;
cout <<n <<endl;
}
}

201没有被判断,直接出循环了。

对啊,你for循环里只有一个continue语句,所以循环到200都是在执行continue 然后当n=201时候跳出了for循环,所以n是等于201

你的程序有问题吧?
if(n%3==0)
continue;
cout <<n <<endl;
为什么不在 for(int n=100;n<=200;n++){
这个{}程序块里?
它应该通不过编译的啊。因为n 离开了for(int n=100;n<=200;n++) 就是没有定义的了。
你是如何让它通过编译的?