Can you do "more or less than" (< >) with switch statements?

I am trying to do more or less but i'm getting an error. I don't know if that's the limit of the function or some special syntax is needed:

switch (var) {
    case < 2:
      //do something when var less than 2
      break;
    case >= 2 && case < 5:
      //do something when var more or equals to 2 and less than 5
      break;
    default: 
      // if nothing else matches, do the default
      // default is optional
    break;
  }

Your syntax is completely wrong. Just look up a reference, it will save you the time. The case part should compare a constant with the value you used in switch. Which is not what you have done. The answer to the title question, is "no".

aarg:
Your syntax is completely wrong. Just look up a reference, it will save you the time. The case part should compare a constant with the value you used in switch. Which is not what you have done. The answer to the title question, is "no".

Ah ok then so switch statements isn't made to handle these types of comparisons. Alright, thanks.

"More or less than" is the same as "not equal to"

Weedpharma

Also, compound comparison statements like (val >= 2 && < 5) are not allowed in C. It has to have separate comparisons, like (val >= 2 && val < 5)

aarg:
Also, compound comparison statements like (val >= 2 && < 5) are not allowed in C. It has to have separate comparisons, like (val >= 2 && val < 5)

That was the thing, you can't type case twice in a switch statement case.

TobiasRipper:
But that was the thing, you can't type case wise in a switch statement case.

No, that was another thing. If I understand you correctly. Perhaps you should explain what, "type case wise" means...
What I meant, is that you can not even do what you did, in an if statement.

aarg:
No, that was another thing. If I understand you correctly. Perhaps you should explain what, "type case wise" means...
What I meant, is that you can not even do what you did, in an if statement.

Oh no I've already done this with the if statement, it works fine there. The switch case was something I wondered for a while though.

I mean you can't do
case >= 2 && case < 5:

TobiasRipper:
Oh no I've already done this with the if statement, it works fine there. The switch case was something I wondered for a while though.

You are mistaken, it can't work.

aarg:
You are mistaken, it can't work.

Ok perhaps I'm posting corrections at the wrong time.

YES, you can do:
variable > 2 && variable < 10

but NO I can't do:
case > 2 && case < 10

This is why I didn't write "case" twice in the code example in the initial post. If I do, Switch statement throws an error.

Just read reply #4 again, carefully. You will see what I mean. It has nothing to do with case. If you already understand that, then you're fine.

aarg:
Just read reply #4 again, carefully. You will see what I mean. It has nothing to do with case. If you already understand that, then you're fine.

I know what you meant with your correction, that's exactly what I just wrote:

I can't do "value > 2 && < 20"

It has to be "value > 2 && value < 20"

I Know.... that's not what I am asking in this thread. Make one arbitrary typo and everyone loses sight of what the actual question is. Syntax Nazis. :smiling_imp:

TobiasRipper:
Syntax Nazis. :smiling_imp:

Hey, I didn't make the rules. Blame K&R. I never lost sight of the original question, as I answered it fully in reply #1. Editing your original post also makes my responses look like you said something up front that I just didn't get. In general, you shouldn't do that.

aarg:
Hey, I didn't make the rules. Blame K&R. I never lost sight of the original question, as I answered it fully in reply #1. Editing your original post also makes my responses look like you said something up front that I just didn't get. In general, you shouldn't do that.

well gotta clean up this mess I made now.

TobiasRipper:
well gotta clean up this mess I made now.

Easy, it's a nested if else structure...

The C case statement is very inflexible as you have now learned.
Often you need to revert to the following construct:

if ( ) {
}
else if ( ) {
}
// ...
// ...
else {
}

The original answer is correct. However, an extension in gcc allows for a range of values to be specified in the case statement. For example, standard C multiple label case

switch (value)
{
  case 1:
  case 2: 
  case 3:
    // do something
...
}

can be written

switch (value)
{
  case 1 ... 3:
    // do something
...
}

Again to emphasize - this is an extension to the language for gcc, but in my mind an eminently sensible one.

I'm not sure if the original post was an actual case, or an example. As an actual case, the switch statement would be extreme overkill. As an if else it would be:

if (var < 2)
{ //... }
else if (var < 5)
{ //... }
else
{ //... }

It seems a lot more straightforward to me.

TobiasRipper:
I am trying to do more or less but i'm getting an error. I don't know if that's the limit of the function or some special syntax is needed:

Switch doesn't do that. In machine code, it compiles down to a goto with a table.

But you can do this:

switch (var) {
    case 0:
    case 1:
      //do something when var less than 2
      break;
    case 2:
    case 3:
    case 4:
      //do something when var more or equals to 2 and less than 5
      break;
    default:
      // if nothing else matches, do the default
      // default is optional
    break;
  }

https://www.arduino.cc/en/Reference/SwitchCase

Link to that is here:

You should bookmark the Arduino Reference Page and probably the Arduino Libraries page and all the site reference-type pages with info that you don't know very well.