Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple conditions in switch case?

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 }
like image 899
user982209 Avatar asked Dec 27 '11 11:12

user982209


People also ask

Can switch case have multiple conditions?

A switch statement includes literal value or is expression based. A switch statement includes multiple cases that include code blocks to execute. A break keyword is used to stop the execution of case block. A switch case can be combined to execute same code block for multiple cases.

Can switch case have multiple conditions in JavaScript?

The JavaScript switch case is a multiple if else statement. It takes a conditional expression just like an if statement but can have many conditions—or cases—that can match the result of the expression to run different blocks of code.


1 Answers

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

.

like image 61
Mike Nakis Avatar answered Oct 11 '22 07:10

Mike Nakis