C++ 没有中断的 switch-case 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33222437/
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-28 14:13:41  来源:igfitidea点击:

switch-case statement without break

c++switch-statement

提问by Lost1

According to this book I am reading:

根据我正在阅读的这本书:

Q What happens if I omit a break in a switch-case statement?

问:如果我在 switch-case 语句中省略了 break 会怎样?

A The break statement enables program execution to exit the switch construct. Without it, execution continues evaluating the following case statements.

A break 语句使程序执行能够退出 switch 结构。没有它,执行将继续评估以下 case 语句。

Suppose if I have codes looking like

假设我的代码看起来像

switch (option}{
    case 1:
    do A;
    case 2:
    do B;
    default:
    do C;
    break;
}

Does this mean if I choose case 1, the A and C are done. If I choose case 2, B and C are done. If i choose neither, then only C is done.

这是否意味着如果我选择案例 1,A 和 C 就完成了。如果我选择案例 2,B 和 C 就完成了。如果我两者都不选择,则只完成 C。

if so, what happens if we omit the break after do C.

如果是这样,如果我们在 do C 之后省略 break 会发生什么。

I assume these are bad programming practice, but I am curious what would happen to get a deeper understanding how it all works. Thanks

我认为这些是不好的编程实践,但我很好奇会发生什么才能更深入地了解它是如何工作的。谢谢

采纳答案by Craig Mosey

The breakacts like a gotocommand. Or, as a better example, it is like when using returnin a voidfunction. Since it is at the end, it makes no difference whether it is there or not. Although, I do like to include it.

这些break行为就像一个goto命令。或者,作为一个更好的例子,就像returnvoid函数中使用一样。既然到了最后,那有没有就没有区别了。虽然,我确实喜欢包括它。

回答by Shoe

You execute everything starting from the selected case up until you see a breakor the switchstatement ends. So it might be that only C is executed, or B and then C, or A and B and C, but never A and C

您从选定的案例开始执行所有操作,直到您看到 abreakswitch语句结束。所以可能只执行 C,或者 B 然后 C,或者 A 和 B 和 C,但从来没有 A 和 C

回答by Khatri

  • If you don't include break in any of case then all the case below will be executed and until it sees break.

  • And if you don't include break in default then it will cause no effect as there are not any case below this 'Default' case.

  • And not using break generally considered as a bad practice but some time it may also come handy because of its fall-through nature.For example:

    case optionA:

    //optionA needs to do its own thing, and also B's thing.
    //Fall-through to optionB afterwards.
    //Its behaviour is a superset of B's.
    

    case optionB:

    // optionB needs to do its own thing
    // Its behaviour is a subset of A's.
    break;
    

    case optionC:

    // optionC is quite independent so it does its own thing.
    break;
    
  • 如果您在任何情况下都不包括 break,那么下面的所有情况都将被执行,直到它看到 break。

  • 如果您不包括默认中断,那么它不会造成任何影响,因为在此“默认”情况下没有任何情况。

  • 并且不使用 break 通常被认为是一种不好的做法,但有时它也可能会派上用场,因为它具有穿透性。例如:

    案例选项A:

    //optionA needs to do its own thing, and also B's thing.
    //Fall-through to optionB afterwards.
    //Its behaviour is a superset of B's.
    

    案例选项B:

    // optionB needs to do its own thing
    // Its behaviour is a subset of A's.
    break;
    

    案例选项C:

    // optionC is quite independent so it does its own thing.
    break;
    

回答by Adham Gamal

switch (option}{
    case 1:
    do A;
    case 2:
    do B;
    case 2:
    do C;
    break;  
    default:
    do C;
}

if your option is 1it executes everything til it finds the breakkeyword... that mean break end the excution of the switch--> caseOutput :A then B then C so it is recommended to put break after each case like :

如果您的选择是1它执行所有内容直到找到break关键字...这意味着中断结束switch-->case输出的执行:A 然后 B 然后 C 所以建议在每个案例之后放置 break ,例如:

switch (option}{
        case 1:
        do A;
        break;
        case 2:
        do B;
        break;
        do C;
        break;        
        default:
        do D;
    }

if your option is 1the Output will be : just A ...

如果您的选择是1输出将是:只是 A ...

note: defaultdoesn't need a break;

注意:default不需要break;

回答by fpierrat

I've seen in many comments and answers that it's a bad practice to omit breaklines. I personally find it very useful in some cases.

我在许多评论和答案中看到,省略break行是一种不好的做法。我个人认为它在某些情况下非常有用。

Let's just take a very simple example. It's probably not the best one, just take it as an illustration:
- on bad login, you need to log the failed attempt.
- for the third bad attempt, you want to log and do some further stuff (alert admin, block account, ...).

让我们举一个非常简单的例子。这可能不是最好的,只是作为一个例子:
- 在错误登录时,您需要记录失败的尝试。
- 对于第三次错误尝试,您想登录并做一些进一步的事情(警报管理员,阻止帐户,...)。

Since the action is the same for first and second try, no need to breakbetween these two and rewrite the same commands a second time.
Now the third time, you want to do other things AND also log. Just do the other things first, then let it run (no break) through the log action of the first and second attempts:

由于第一次和第二次尝试的动作相同,因此无需break在这两者之间再次重写相同的命令。
现在第三次,您想要做其他事情并且还要登录。只需先做其他事情,然后让它break通过第一次和第二次尝试的日志操作运行(否):

switch (badCount) {
    case 3: //only for 3
        alertAdmin();
        blockAccount();
    case 2: //for 2 AND 3
    case 1: //for 1 AND 2 and 3
        errorLog();
        badCount++;
}

Imho, if it was soooo bad practice to have common code for different cases, the C structure would simply NOT allow it.

恕我直言,如果针对不同情况使用通用代码是非常糟糕的做法,那么 C 结构将根本不允许这样做。

回答by soch guru

The key is execution control is transferred to the statement for the matching case. E.g.

关键是执行控制转移到匹配案例的语句。例如

1. switch(x) {
2.   case 1:
3.      do_step1;
4.   case 2:
5.      do_step2;
6.   default:
7.      do_default;
8.   }

Treat lines 2, 4, 6, as "Labels" for the goto calls. On x = 1, the control will be transferred to line 3 & execution of line 3, 5 & 7 will occur.

将第 2、4、6 行视为 goto 调用的“标签”。在 x = 1 时,控制将转移到第 3 行,并且将执行第 3、5 和 7 行。