Switch - Case versus If then, elseif, etc

Hi Guys,

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?

C++ switch case does not offer ranges typically but our compiler added a simplified writing for ranges with three dots

case 100 ... 200:
  // do something if value is in [100, 200] interval
  break;

(Be careful: Write spaces around the ... for the parser to not be mislead )

PS: @paulpaulson don’t put a lot of faith in the very simplified (dumbed down) arduino documentation.

1 Like

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.

1 Like

You may need brackets to isolate the comparison. The compiler will may drop warnings.
Check 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...

What’s wrong with the answers you got?
I gave you a way to do it with switch/case and the if/else is totally fine too… (answers 3 and 4)

Can you rephrase your question?

Can you explain yourself, Paul?

@J-M-L ,
I do not recognize the tropple dot thingy... Is it a common code??
Or does it require a library or something?

Have you clicked the GCC link ? It’s built into the compiler although not a standard c++ feature

Cf Case Ranges (Using the GNU Compiler Collection (GCC))

Paul meant to write

if( (var > 20) and (var < 50) ) {

To make it obvious where the and applies

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.

Eg:

1/2, 1/4, 1/8 ... 1/32

@FTMZ, I'm sorry if I'm to harsh in this reply but if you read all the replies, then you have a pretty good overall nuanced view.

Still interested in an explanation of your statement….

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. :slight_smile:

Ok

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.

Well, I think the TO is able tp find the corrent answer to his question.

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.

Thanks guys, for the effort. :blush: :nerd_face:

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.

You can also use:

case 1:
case 2:
case 3:
// Some code
break;
// Etc

That will run the same code for cases 1, 2 and 3. You can do as many as you like together like that.

That’s why GCC came up with the ellipsis thingy… saves you typing a long list :wink:

1 Like