I have a statement that if a certain variable is i.e. 5 it should do something, and with 4 it should do something else, right?
I know that if there are quite some "if- then" situations, "Switch-Case" would be handy.
But how can I create that situations with statements that if something is within a certain range, action A should be taken, and within another range, action B should be taken?
I tried:
switch (var) {
case (< 50) and (>20):
// do action A
break;
case (< 122) and (>67):
// do action B
break;
default:
// if nothing fits, then do this
break;
}
But that does not seem to work.
Just a quick question to get me back on track again...
Is a range of value's suitable for the Switch-Case statements?
The switch-case and if-statements of the 'C' language go back to the possibilities of the processor when the 'C' language was developed. The switch-case is a jump table and the if-statements are conditional tests.
The range with three dots for a switch-case might give a warning.
I think you need if-statements. You can use the alias 'and' instead of '&&' to make it better to read.
if( var > 20 and var < 50)
{
// action A
}
else if( var > 67 and var < 122)
{
// action B
}
else
{
// nothing fits
}
That is perfectly good code. Good to read. It shows what it does. Nothing wrong with it.
I'm sorry if I'm to harsh in this reply, but would you be so kind to at least read the question instead of reading just the main titel and judge the whole situation by it.
I do appreciate your effort, but you can emagine i've looked into that allreacy.
Reading my startpost would give the right impression, namely that I did not find a solution to cover a range of values rather than a single value...
The "triple dot thing" is called an ellipsis and is used in C++ and other languages to denote a series. It is also used in mathematics to denote a series.
Easy...
I'm asking a question of something I do not understand.
Knowing the drills I post an example code which makes it clear I have troubles with the Switch Case statements if it contains a range of variables.
That was the question I asked. I even cut'n pasted the example code from the referance page and adjusted it to fit my question.
So... I made clear what I want. And that I searched for info...
Then why the simple copy-paste link? A link that doesn't even contain the solution for my problem.
Which replies?
I never mentioned that the replies posted áfter the unusefull link were not helpfull.
I'm eager to learn. Still reading and asking questions in response.
I think @paulpaulson (correct me if I am wrong) wanted to point out that the C++ specification does not support ranges. So the answer to your question was there: In pure c++ you would go for the if/else solution.
The fact that GCC supports it is very specific and compiler dependent so it’s an option that comes with strings attached.
OK, the Switch-Case situation is not able to work with a range of values.
At least not without other applications (I do not know how to apply extra applications or codes to make things more easy. Is it done with library's then?)
So I can skip that idea and therefore make use of the If, Else If and Else.
No big deal and easy to comprehend.
I just thought a Switch-Case (or Select-Case as I recall from PICbasic) would be a better solution.
The range based case i gave you WILL work on your Arduinos. It’s just not standard C++. There is nothing to install. You can use it or you can use the if/else.