Pages: [1]   Go Down
Author Topic: Question on Switch case statement  (Read 226 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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;
 
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2373
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No.
you cam do lists of valueseg
Code:
case 1:
case 2:
case 3:
Mark
Logged

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 105
Posts: 4681
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No.
you cam do lists of valueseg
Code:
case 1:
case 2:
case 3:
Mark

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

The Elders of the Internet know who I am
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Offline Offline
God Member
*****
Karma: 15
Posts: 508
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No.
you cam do lists of valueseg
Code:
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.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 70
Posts: 3727
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There's also the case range that gcc supports:
Code:
   case 2 ... 5:
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 221
Posts: 13848
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

simple example of the options of switch
Code:
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);
}
Logged

Rob Tillaart

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you all for your responses!
Logged

Pages: [1]   Go Up
Jump to: