Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

advanced switch case

I want to use a variable in a switch case. as an example, could not do it. Can you help?

switch(basket,pay){
   case true, true:
       blah blah...
   break;
   case false,true:
       blah blah..
   break;
   case false, false:
      blah blah...
   break;
}
like image 207
Escort42 Avatar asked Dec 01 '22 22:12

Escort42


1 Answers

What you're looking for is flags and bitmasks. Basically you store myltiple pseudo-boolean values insdie the same variable and you can access their compound condition. The beauty of this is that you can compound multiple properties into a single variable. This is especially neat when there are a lot more boolean properties which you need to keep track of

var FLAG_BASKET = 0x1; // 0001  
var FLAG_PAY = 0x2; // 0010

//set them both to pseudo-true
var flags = FLAG_BASKET | FLAG_PAY;

switch (flags){
  case 0x0://both are false
  case 0x1://basket is true, pay is false
  case 0x2://basket is false, pay is true
  case 0x3://both are true
}

Fiddled

like image 166
Oleg Avatar answered Dec 10 '22 12:12

Oleg