Switch case syntax error

Will someone please let me know how to correct the error shown in the attached switch case code.
Thank you

Please don't post images of code.. it makes it hard for anyone to debug.

int d;

void setup()
{
  switch (d)
  {
    case 0 ... 5:
      break;
    case 6 ... 10:
      break;
    case 11 ... 15:
      break;
    case 16 ... 20:
      break;
    default:         // > 20
      break;
  }
}

void loop()
{

}
2 Likes

you forgot the last

  default:
    // statements
    break;

Thank you and thanks for reminding me to paste code directly from the app.

the switch structure doesnt allow a range to be represented in that way.

Note - in specifying a range you must use this exact format

case (low value) space three dots space (high value): as shown above.

I'm sure if you delete all but the first case it will still show a compiler error.

You DONT need to have a "default " if the cases cover all possibilities - but in your case if d can exceed 25 its better to have one.

Many thanks johnerrington. I did manage to get it to compile when I removed the range and assumed switch case would only accept a single integer, Your tutorial is the perfect solution. I didn't see that in the switch case reference documentation. Very much appreciate you help. Thank you.

1 Like

The 'default:' case is not required.
The final 'break;' is not required.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.