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.
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.
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.
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
{ //... }
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;
}
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.