C++ 在 switch 语句中使用 continue

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2146763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:16:31  来源:igfitidea点击:

Using continue in a switch statement

c++cswitch-statementbreakcontinue

提问by Matt Joiner

I want to jump from the middle of a switchstatement, to the loop statement in the following code:

我想从switch语句的中间跳转到以下代码中的循环语句:

while (something = get_something())
{
    switch (something)
    {
    case A:
    case B:
        break;
    default:
        // get another something and try again
        continue;
    }
    // do something for a handled something
    do_something();
}

Is this a valid way to use continue? Are continuestatements ignored by switchstatements? Do C and C++ differ on their behaviour here?

这是一种有效的使用方式continue吗?continue语句是否被语句忽略switch?C 和 C++ 在这里的行为是否有所不同?

采纳答案by visitor

It's fine, the continuestatement relates to the enclosing loop, and your code should be equivalent to (avoiding such jump statements):

没关系,该continue语句与封闭循环有关,并且您的代码应等效于(避免此类跳转语句):

while (something = get_something()) {
    if (something == A || something == B)
        do_something();
}

But if you expect breakto exit the loop, as your comment suggest (it always tries again with another something, until it evaluates to false), you'll need a different structure.

但是,如果您希望break退出循环,正如您的评论所暗示的那样(它总是用另一个东西再次尝试,直到它评估为假),您将需要一个不同的结构。

For example:

例如:

do {
    something = get_something();
} while (!(something == A || something == B));
do_something();

回答by visitor

Yes, it's OK - it's just like using it in an ifstatement. Of course, you can't use a breakto break out of a loop from inside a switch.

是的,没关系 - 就像在if语句中使用它一样。当然,您不能使用 abreak从交换机内部跳出循环。

回答by Islam Elshahat

Yes, continue will be ignored by the switch statement and will go to the condition of the loop to be tested. I'd like to share this extract from The C Programming Language reference by Ritchie:

是的,continue 将被 switch 语句忽略,并会转到要测试的循环条件。我想分享 Ritchie 的 The C Programming Language 参考中的摘录:

The continuestatement is related to break, but less often used; it causes the next iteration of the enclosing for, while, or doloop to begin. In the whileand do, this means that the test part is executed immediately; in the for, control passes to the increment step.

The continue statement applies only to loops, not to a switchstatement. A continueinside a switchinside a loop causes the next loop iteration.

continue语句与 相关break,但较少使用;它会导致封闭的下一次迭代forwhiledo循环开始。在whileand 中do,这意味着测试部分立即执行;在 中for,控制传递到增量步骤。

continue 语句仅适用于循环,不适用于switch语句。A continueinside a switchinside 循环导致下一次循环迭代。

I'm not sure about that for C++.

对于 C++,我不确定这一点。

回答by SF.

It's syntactically correct and stylistically okay.

它在语法上是正确的,在风格上也没有问题。

Good style requires every case:statement should end with one of the following:

好的风格要求每条case:语句都应以以下之一结尾:

 break;
 continue;
 return (x);
 exit (x);
 throw (x);
 //fallthrough

Additionally, following case (x):immediately with

此外,case (x):立即跟随

 case (y):
 default:

is permissible - bundling several cases that have exactly the same effect.

是允许的 - 捆绑几个具有完全相同效果的案例。

Anything else is suspected to be a mistake, just like if(a=4){...}Of course you need enclosing loop (while, for, do...while) for continueto work. It won't loop back to case()alone. But a construct like:

其他任何东西都被怀疑是错误的,就像if(a=4){...}当然你需要封闭循环 ( while, for, do...while)continue才能工作。它不会循环回到case()单独。但是像这样的构造:

while(record = getNewRecord())
{
    switch(record.type)
    {
        case RECORD_TYPE_...;
            ...
        break;
        default: //unknown type
            continue; //skip processing this record altogether.
    }
    //...more processing...
}

...is okay.

...没关系。

回答by Alexander Poluektov

While technically valid, all these jumps obscure control flow -- especially the continuestatement.

虽然技术上有效,但所有这些跳转都掩盖了控制流——尤其是continue语句。

I would use such a trick as a last resort, not first one.

我会使用这样的技巧作为最后的手段,而不是第一个。

How about

怎么样

while (something = get_something())
{
    switch (something)
    {
    case A:
    case B:
        do_something();
    }        
}

It's shorter and perform its stuff in a more clear way.

它更短,并以更清晰的方式执行其内容。

回答by Adrian S

This might be a megabit to late but you can use continue 2.

这可能有点晚了,但您可以使用continue 2.

Some php builds / configs will output this warning:

某些 php 构建/配置将输出此警告:

PHP Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"?

PHP 警告:“continue”定位开关等同于“break”。您的意思是使用“继续 2”吗?

For example:

例如:

$i = 1;

while ($i <= 10) {
    $mod = $i % 4;
    echo "\r\n out $i";
    $i++;
    switch($mod)
    {
        case 0:
            break;
        case 2:
            continue;
            break;
        default:
            continue 2;
            break;
    }
    echo " is even";
}

This will output:

这将输出:

out 1
out 2 is even
out 3
out 4 is even
out 5
out 6 is even
out 7
out 8 is even
out 9
out 10 is even

Tested with PHP 5.5 and higher.

使用 PHP 5.5 及更高版本进行测试。

回答by Jitendra Nagar

Switch is not considered as loop so you cannot use Continueinside a case statement in switch...

Switch 不被视为循环,因此您不能在 switch 的 case 语句中使用Continue...