continue可以出现在switch语句中吗?为什么?

kenryHuang 2002-09-21 10:36:02
我写下以下语句
int a = 1;
switch(a) {

case 1:
continue;
case 2:
break;
}

编译错误:非法的continue语句。(VC++)
continue只能在for中出现(Dev-C++)

对于ANSI C++有这一个标准吗,哪里可以找到?
kenryhuang(strayer of midnight)
...全文
2218 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
gqlin 2002-09-22
  • 打赏
  • 举报
回复
switch中可以放的,只要这个switch在for或while循环内如:
while (1) {
switch(a) {
1:
continue;
break;
2:
break;
}
break;
}
网事随逢 2002-09-22
  • 打赏
  • 举报
回复
he
sandrowjw 2002-09-22
  • 打赏
  • 举报
回复
continue直接到达循环的尾部(跳过以下的语句,然后由尾部的转回指令进入下一次循环条件的判断程序(这段程序一般是在尾部的)。对于分支指令,没有所谓的尾部的判断程序,唯一的判断是在头部的,你让它怎么continue?
break是跳到统一的“非”出口,这个对于分支和循环是一样的。
wangmin_yjitx 2002-09-22
  • 打赏
  • 举报
回复
continue;就是循环中执行到这里时,不再执行循环体下面的语句,执行下一次循环。好象在回溯算法中用的较多。
kenryHuang 2002-09-21
  • 打赏
  • 举报
回复
blh(老猫) :
break可以用在switch中吧。。这应该是肯定的。
wlpwind(同风起) :
我写这个程序没什么意思,也不想达到什么效果
我在另一种脚本语言中,尝试这个语句,结果造成了死循环
我只是想在C中求证一下,我想看看:在一般程序设计的语义
中,在switch中加入continue是否为合法的?如果合法会
达到什么样的结果??



谢谢以上各位!

以上各位都指出:continue语句在一般用法上来说,都是放
在循环语句中用来直接进行下一次循环。这个当然是很清楚
的问题。

但是我想知道的是:continue语句放在switch语句中是否就是
不合法的???根据编译错误来说,这应该成立,但是标准
C++中有说到这一点吗?

毕竟一件事情的合法并不能证明另一件事情的不合法。
mylove0618 2002-09-21
  • 打赏
  • 举报
回复
The continue statement passes control to the next iteration of the do, for, or while statement in which it appears, bypassing any remaining statements in the do, for, or while statement body. A typical use of the continue statement is to return to the start of a loop from within a deeply nested loop.

Syntax

jump-statement :

continue;

The next iteration of a do, for, or while statement is determined as follows:

Within a do or a while statement, the next iteration starts by reevaluating the expression of the do or while statement.


A continue statement in a for statement causes the first expression of the for statement to be evaluated. Then the compiler reevaluates the conditional expression and, depending on the result, either terminates or iterates the statement body. See The for Statement for more information on the for statement and its nonterminals.
This is an example of the continue statement:

while ( i-- > 0 )
{
x = f( i );
if ( x == 1 )
continue;
y += x * x;
}

In this example, the statement body is executed while i is greater than 0. First f(i) is assigned to x; then, if x is equal to 1, the continue statement is executed. The rest of the statements in the body are ignored, and execution resumes at the top of the loop with the evaluation of the loop’s test.
-------------------------------------------------msdn
从上面的情况来看,continue只适合用在循环中。
wlpwind 2002-09-21
  • 打赏
  • 举报
回复
continue 用来结束一次循环。
你写的程序什么意思?
blh 2002-09-21
  • 打赏
  • 举报
回复
continue,break用于循环语句中,呵呵,这是c语言语法挥定的
dajian2000 2002-09-21
  • 打赏
  • 举报
回复
我认为,在switch 中不需要continue .
在switch中,程序是顺序执行的,执行完了case1 ,就直接case2,当然,如果case1后面有break;的话,就跳出,否则做case2,所以不需要continue.
wlpwind 2002-09-21
  • 打赏
  • 举报
回复
据我所知,continue 是用于循环结构中的。
应该不止局限于for中的,我觉得在 do while 和while中也能用。
quickball 2002-09-21
  • 打赏
  • 举报
回复
关注!
alexxing 2002-09-21
  • 打赏
  • 举报
回复
continue 的意思就是:直接跳转到下一次循环

——你说合法不合法?
julyclyde 2002-09-21
  • 打赏
  • 举报
回复
我认为continue即使可以在switch里用,从语义上讲也通不过
kenryHuang 2002-09-21
  • 打赏
  • 举报
回复
csz_cmy(讨厌编程):
我明白你的意思
C++不会规定所有的语句是不是错的。
它一般只会规定那些写法是对的。

我说的意思是象上面那种语句
switch(a) {
case 1:
continue;
}

这种东西从语法上来猛一看没有错误,
而实际上是错的,所以导致了我的困惑。

另外,你说话时语气一定要这么强硬吗?
我接受你的教育,但讨厌你说话的方式。
你连几个字都写不对,看样子也没什么大出息。
我还不屑于和你这种人一般见识
superzjx2000 2002-09-21
  • 打赏
  • 举报
回复
没有流程控制语句(while /if/for/do while)
boulder 2002-09-21
  • 打赏
  • 举报
回复
不用在这里兜圈,一用就知道了
windysys 2002-09-21
  • 打赏
  • 举报
回复
同意楼上的楼上.....就是白字多了些.....
lx_cyh 2002-09-21
  • 打赏
  • 举报
回复
是这样地,switch语句如果是循环体的一部分,则可以有continue语句,实际上
continue语句还是与循环语句合作的.否则switch语句中不可以有continue语句
"但是我想知道的是:continue语句放在switch语句中是否就是
不合法的???根据编译错误来说,这应该成立,但是标准
C++中有说到这一点吗?"
csz_cmy 2002-09-21
  • 打赏
  • 举报
回复
搂主

我问你,"你去死吧!"庸才C++中合法吗?
C++ 的书没说他不黑发!!!

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧