Multiple conditions in switch case?

103,105

Solution 1

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

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

But you can use multiple switches:

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

Solution 2

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

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.

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

It is hacky, but it may come in handy.

The trick is to convert the true/false value of each of your conditions to a bit, concatenate these bits into an int value, and then switch on the int value.

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 case label fall through if you have either-or scenarios. For example:

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.
}

.

Solution 3

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

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

Solution 4

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.

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.

Share:
103,105
user982209
Author by

user982209

Updated on August 30, 2020

Comments

  • user982209
    user982209 over 3 years

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

    switch (conditionA or conditionB fullfilled)
      { //execute code }
    
    • Roee Gavirel
      Roee Gavirel over 12 years
      what would be the advantage of the switch over a normal if? can you give an example of what you would do with it if it was possible?
    • user982209
      user982209 over 12 years
      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.