user46158: in pascal's case statement i can use this:
Var i : integer;  
...  
Case i of   
 1..5 : DoSomething;  
end;

i was wondering if there is a way in can do this with c/c++ in the arduino compiler using:
switch (var) {
   case 1..5:
     // statements
     break;

or is this only possible using if/else?

user10394: Short answer: No
Longer answer: Each case in a switch statement must be a discrete value (which is why floats and doubles won't work either) but there are way around it.  In order of preference:
A) Utilise the "drop through" feature
switch (value)
{ 
  case 1: // Deliberate drop through
  case 2: // Deliberate drop through
  case 3: // Deliberate drop through
  case 4: // Deliberate drop through
  case 5:
    DoThingA();
    break;
  case 6:
    DoThingB();
    break;
}

B) Change the logic so the range is the default  (not nice, but if you have no other option):
switch (value)
{ 
  case 6:
    DoThingB();
    break;
  default:
    DoThingA();
    break;
}

C) Use a helper function (Really ugly, but...).  There would have to be a good reason not to call the DoThing() from the helper function.
enum DoOptions {EOptionA, EOptionB};
DoOptions DoWhat (int value)
{
  if (value < 6) return EOptionA;
  return EOptionB;
}
switch (DoWhat(value))
{ 
  case EOptionA:
    DoThingA();
    break;
  case EOptionB:
    DoThingB();
    break;
}


user23841: There are some GNU extensions available in the avr-gcc/avr-g++ compilers (used in Arduino IDE for AVR based Arduino boards). One of them is suport of ... for the case:
  switch (i)
  {
    case  10 ... 100:  Serial.println(F("First"));           break;
    case 101 ... 1000: Serial.println(F("Second"));          break;
    default:           Serial.println(F("Everything else")); break; 
  }

But, it's not the part of C/C++ standard, it's just compiler extension, so there is possibility it fails to compile on different platforms (mainly different compiler). So it's better to avoid it in portable code.

46158: <p>in pascal's case statement i can use this:</p>

<pre><code>Var i : integer;  
...  
Case i of   
 1..5 : DoSomething;  
end;
</code></pre>

<p>i was wondering if there is a way in can do this with c/c++ in the arduino compiler using:</p>

<pre><code>switch (var) {
   case 1..5:
     // statements
     break;
</code></pre>

<p>or is this only possible using if/else?</p>
 10394: <p>Short answer: No</p>

<p>Longer answer: Each case in a switch statement must be a discrete value (which is why floats and doubles won't work either) but there are way around it.  In order of preference:</p>

<p>A) Utilise the "drop through" feature</p>

<pre><code>switch (value)
{ 
  case 1: // Deliberate drop through
  case 2: // Deliberate drop through
  case 3: // Deliberate drop through
  case 4: // Deliberate drop through
  case 5:
    DoThingA();
    break;
  case 6:
    DoThingB();
    break;
}
</code></pre>

<p>B) Change the logic so the range is the default  (not nice, but if you have no other option):</p>

<pre><code>switch (value)
{ 
  case 6:
    DoThingB();
    break;
  default:
    DoThingA();
    break;
}
</code></pre>

<p>C) Use a helper function (Really ugly, but...).  There would have to be a good reason not to call the DoThing() from the helper function.</p>

<pre><code>enum DoOptions {EOptionA, EOptionB};
DoOptions DoWhat (int value)
{
  if (value &lt; 6) return EOptionA;
  return EOptionB;
}
switch (DoWhat(value))
{ 
  case EOptionA:
    DoThingA();
    break;
  case EOptionB:
    DoThingB();
    break;
}
</code></pre>
 23841: <p>There are some GNU extensions available in the avr-gcc/avr-g++ compilers (used in Arduino IDE for AVR based Arduino boards). One of them is suport of <code>...</code> for the <code>case</code>:</p>

<pre><code>  switch (i)
  {
    case  10 ... 100:  Serial.println(F("First"));           break;
    case 101 ... 1000: Serial.println(F("Second"));          break;
    default:           Serial.println(F("Everything else")); break; 
  }
</code></pre>

<p>But, it's not the part of C/C++ standard, it's just compiler extension, so there is possibility it fails to compile on different platforms (mainly different compiler). So it's better to avoid it in portable code.</p>
