Can you do a comparison with a case in a switch?

How do you do comparison with a case within a switch?

   switch (i){
      case (i<3):   
             //Do something while i is less than 3
}

This doesn’t Compile and I’m new… Unfortunately for me the Arduino Ref doesn’t have an example. Which means you cannot do it or I’m doing it wrong. Any pointers appreciated.

No, you can't. The case argument must be a literal constant.

You can do...

   switch (i){
      case 1:   
      case 2:   
             //Do something while i is less than 3
             break;
      case 3:
      case 4:
      case 5:
             //Do something while i is 3 to 5
             break;
}

if that helps.

case 0:
case 1:
case 2:
case 3:

// do same stuff for 0, 1, 2, 3

break;

is allowed.

Current implementations of the Arduio IDE are built around a version of GCC which allows this:

  int value = 3;
  switch ( value )
  {
    case 0 ... 3:
      break;
  }

Thank you Everyone. I really like lloyddean approach that is clean. Thanks again!

if things are complex you could create a function that returns an unique number for the different conditions.

simple example

int i = sign(strcmp(s1, s2));

switch(i)
{
  case -1: // s1 < s2 alphabetically
    break;
  case 0:  // s1==s2
    break;
  case 1:  // s1 > s2 alphabetically
}

Note that enums are allowed in a switch statement

enum Days {Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday};

Days d = f(...);

switch(s)
{
  case Saturday:
  case Sunday: 
    serial.println("Weekend");
    break;
  case Monday:
  case Tuesday:
  case Wednesday:
  case Thursday:
  case Friday:
    serial.println("Work days");
    break;
}