Go Down

Topic: Question on Switch case statement (Read 276 times) previous topic - next topic

Lavan

Is it possible to use Boolean operators with case?  something like below...

case 1 || 2:
do this;
break;

and

case > 5 && < 10:
do this;
break;

holmes4

No.
you cam do lists of valueseg
Code: [Select]
case 1:
case 2:
case 3:

Mark

JimboZA


No.
you cam do lists of valueseg
Code: [Select]
case 1:
case 2:
case 3:

Mark


That's an OR, though: not sure if it's possible to do an AND?

Jiggy-Ninja



No.
you cam do lists of valueseg
Code: [Select]
case 1:
case 2:
case 3:

Mark


That's an OR, though: not sure if it's possible to do an AND?

It's not possible to be more than one case at a time, so OR is all you need. His AND example was just a range greater than 5 and less then 10, so the values 6-9 satisfy it.

wildbill

There's also the case range that gcc supports:
Code: [Select]
   case 2 ... 5:

robtillaart

simple example of the options of switch
Code: [Select]

int x;

void setup()
{
  Serial.begin(115200);  // use fastest serial possible
  x = 0;

}

void loop()
{
  x++;

  switch(x)
  {
  case 0:
    Serial.println("zero");
    break;
  case 1 ... 9:
    Serial.println("between 1 and 9");
    break;
  case 10:
  case 11:
  case 13:
    Serial.println(x);
    break;
  case 15:
    x = 0;
    break;
  default:
    Serial.println(".");
  }
  delay(1000);
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Lavan

Thank you all for your responses!

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy