C++ 开关盒中的多个条件?

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

Multiple conditions in switch case?

c++loopsswitch-statement

提问by user982209

Can i use switch case to check multiple condition? like for example either or of the condition fulfilled it will do its case?

我可以使用 switch case 来检查多个条件吗?例如,要么满足条件,要么满足条件?

switch (conditionA or conditionB fullfilled)
  { //execute code }

回答by Mike Nakis

Obviously, the question of how to execute code if either conditionA or conditionB is truecan be trivially answered with if( conditionA || conditionB ), no switchstatement necessary. And if a switchstatement is for some reason a must-have, then the question can again be trivially answered by suggesting a caselabel fall through, as one of the other answers does.

显然,如果条件 A 或条件 B 是如何执行代码的问题,true可以用 简单地回答if( conditionA || conditionB ),不需要任何switch语句。如果某个switch陈述出于某种原因是必须具备的,那么该问题可以再次通过建议case标签失效来简单回答,就像其他答案中的一个一样。

I do not know whether the needs of the OP are fully covered by these trivial answers, but this question will be read by many people besides the OP, so I would like to present a more general solution which can solve many similar problems for which trivial answers simply won't do.

我不知道这些琐碎的答案是否完全涵盖了 OP 的需求,但是除了 OP 之外,这个问题还会被很多人阅读,所以我想提出一个更通用的解决方案,可以解决许多类似的问题答案根本行不通。

How to use a single switchstatement to check the values of an arbitrary number of boolean conditions all at the same time.

如何使用单个switch语句同时检查任意数量的布尔条件的值。

It is hacky, but it may come in handy.

这是hacky,但它可能会派上用场。

The trick is to convert the true/falsevalue of each of your conditions to a bit, concatenate these bits into an intvalue, and then switchon the intvalue.

诀窍是将每个条件的true/false值转换为一个位,将这些位连接成一个int值,然后switch在该int值上。

Here is some example code:

下面是一些示例代码:

#define A_BIT (1 << 0)
#define B_BIT (1 << 1)
#define C_BIT (1 << 2)

switch( (conditionA? A_BIT : 0) | (conditionB? B_BIT : 0) | (conditionC? C_BIT : 0) )
{
     case 0:                     //none of the conditions holds true.
     case A_BIT:                 //condition A is true, everything else is false.
     case B_BIT:                 //condition B is true, everything else is false.
     case A_BIT + B_BIT:         //conditions A and B are true, C is false.
     case C_BIT:                 //condition C is true, everything else is false.
     case A_BIT + C_BIT:         //conditions A and C are true, B is false.
     case B_BIT + C_BIT:         //conditions B and C are true, A is false.
     case A_BIT + B_BIT + C_BIT: //all conditions are true.
     default: assert( FALSE );   //something went wrong with the bits.
}

Then, you can use caselabel fall through if you have either-or scenarios. For example:

然后,case如果您有非此即彼的场景,则可以使用标签失败。例如:

switch( (conditionA? A_BIT : 0) | (conditionB? B_BIT : 0) | (conditionC? C_BIT : 0) )
{
     case 0:
         //none of the conditions is true.
         break;
     case A_BIT:
     case B_BIT:
     case A_BIT + B_BIT:
         //(either conditionA or conditionB is true,) and conditionC is false.
         break;
     case C_BIT:
         //condition C is true, everything else is false.
         break;
     case A_BIT + C_BIT:
     case B_BIT + C_BIT:
     case A_BIT + B_BIT + C_BIT:
         //(either conditionA or conditionB is true,) and conditionC is true.
         break;
     default: assert( FALSE );   //something went wrong with the bits.
}

.

.

回答by citxx

No. In c++ switch case can be used only for checking values of one variable for equality:

不可以。在 c++ switch case 中只能用于检查一个变量的值是否相等:

switch (var) {
    case value1: /* ... */ break;
    case value2: /* ... */ break;
    /* ... */
}

But you can use multiple switches:

但是您可以使用多个开关:

switch (var1) {
    case value1_1:
        switch (var2) {
            /* ... */
        }
        break;
    /* ... */
}

回答by Konstantin

What about the fall-through feature of the switch/case construct?

switch/case 结构的 fall-through 特性怎么样?

switch(condition){
    case case1:
        // do action for case1
        break;
    case case2:
    case case3:
        // do common action for cases 2 and 3
        break;
    default:
        break;
}

回答by mczarnek

In response to your comment: Ok I actually want my robot to move forward when either button 1 or 2 is clicked. But somehow the other buttons will just follow the previous direction previously executed.

回应您的评论:好的,我实际上希望我的机器人在单击按钮 1 或 2 时向前移动。但不知何故,其他按钮只会遵循先前执行的先前方向。

You could simply AND together whether the first button is clicked with whether the second button is clicked, then use a single switch case or if statement.

您可以简单地将是否单击第一个按钮与是否单击第二个按钮放在一起,然后使用单个 switch case 或 if 语句。