# Thread: Quick question concerning switch statements

1. ## Quick question concerning switch statements

This code won't work, I'm assuming it's because the case can't be a logical statement.

The actual program I'm working on would use a much more complex switch than this, this is simply a small example of the concept.

Code:
```#include <iostream>
using namespace std;

int main( )
{
int x = 1;
switch (x)
{
case (0 || 1):
cout << "X is either 0 or 1" << endl;
break;
case (1 || 2):
cout << "X is either 1 or 2" << endl;
break;
case (2 || 3):
cout << "X is either 2 or 3" << endl;
break;
}
system("pause");
return 0;
}```
My question is, what's the easiest way to accomplish similar results?

Ideally, I'd want the output generated to be

X is either 0 or 1
X is either 1 or 2

Thanks 2. Switches have to be constant...so you could just use a series of ifs 3. Originally Posted by prog-bman Switches have to be constant...so you could just use a series of ifs
There's no way around that?

I chose switches because for what I'm doing, the if statements will be MUCH slower... and there's a lot of data. 4. You can use the fall-through property of switches:

Code:
```switch (x) {
case 0:
case 1:
cout << "X is either 0 or 1" << endl;
break;```
From there on, since 1 has already been handled, in the next hypothetical case "1 or 2" the value would never be 1 - it can only be 2.

However, there are certainly possibilities to write the above program without a switch or an if-else chain (keeping the messages somewhat broken as in the original code).

Code:
```int main()
{
std::string messages[] = {"X is either 0 or 1" , "X is either 0 or 1" , "X is either 1 or 2", "X is either 2 or 3"};
int x = 1;

//a range check should go here: the default case
std::cout << messages[x] << std::endl;
}```
Polymorphism is another means of replacing switches. 5. Originally Posted by Shaun32887 There's no way around that?

I chose switches because for what I'm doing, the if statements will be MUCH slower... and there's a lot of data.
No, there is no way around the way the language is defined. They must be constant values.

Your understanding of switches and if statements and their speed seems to be a bit lacking. A switch statement is only beneficial in terms of speed of execution because of this fact that the compiler knows exactly where to jump to in the generated machine code based upon a single number in a single variable. A series of if statements could theoretically be compiled to be just as fast if they are written in the same manner. 6. Duplicate post, see bottom reply. 7. Originally Posted by MacGyver No, there is no way around the way the language is defined. They must be constant values.
I didn't mean I was trying to get around the way the language was defined. I was simply asking the more experienced programmers if there was a quick way that would produce the same results. The switch statement suggestion was similar to what I was looking for, but as I said, it would take a long time because it would have to be executed more times in the loop. Originally Posted by MacGyver Your understanding of switches and if statements and their speed seems to be a bit lacking.
It's not how fast the if statement executes versus how fast the switch statement executes, it's the number of times the statements will have to be run. With the switch statement, I can get it all done in 1 pass and the computer would only be making three decisions per input.

Then again I am pretty tired and I might be able to make it work. Hmm... 8. Originally Posted by anon You can use the fall-through property of switches:

Code:
```switch (x) {
case 0:
case 1:
cout << "X is either 0 or 1" << endl;
break;```
Well, while it would have to fall through exactly 1 case, I'd have to set up some kind of flag and then if then break thing. That actually could work, I'll look into this. Originally Posted by anon However, there are certainly possibilities to write the above program without a switch or an if-else chain (keeping the messages somewhat broken as in the original code).

Code:
```int main()
{
std::string messages[] = {"X is either 0 or 1" , "X is either 0 or 1" , "X is either 1 or 2", "X is either 2 or 3";
int x = 1;

//a range check should go here: the default case
std::cout << messages[x] << std::endl;
}```
Polymorphism is another means of replacing switches.

Sorry, I didn't see all of this before.

My crash course on c++ skipped polymorphism, I'll check it out.

This is exactly what I was looking for, thanks for the help. 9. With the switch statement, I can get it all done in 1 pass.
I don't understand that. You should be able to turn any switch statement into an if-else chain (well, fallthroughs might need goto, unless you can combine the cases with boolean operators). 10. Originally Posted by anon I don't understand that. You should be able to turn any switch statement into an if-else chain (well, fallthroughs might need goto, unless you can combine the cases with boolean operators).
Yeah, you're right. I was thinking of another less efficient way to write the if statements.

Still, I would have the same problem.

Code:
```#include <iostream>
using namespace std;

int main( )
{
int x = 1;

if (x == 0 ||x == 1)
cout << "X is either 0 or 1" << endl;
else if (x == 1 ||x == 2)
cout << "X is either 1 or 2" << endl;
else if (x == 2 ||x == 3)
cout << "X is either 2 or 3" << endl;
system("pause");
return 0;
}```
This would still only display

"X is either 0 or 1"

Since it won't execute ALL the satisfied branches, just the 1st satisfied branch.

I think the conditional fallthrough might be my solution. 11. Originally Posted by Shaun32887 This code won't work, I'm assuming it's because the case can't be a logical statement.

The actual program I'm working on would use a much more complex switch than this, this is simply a small example of the concept.

Code:
```#include <iostream>
using namespace std;

int main( )
{
int x = 1;
switch (x)
{
case (0 || 1):
cout << "X is either 0 or 1" << endl;
break;
case (1 || 2):
cout << "X is either 1 or 2" << endl;
break;
case (2 || 3):
cout << "X is either 2 or 3" << endl;
break;
}
system("pause");
return 0;
}```
My question is, what's the easiest way to accomplish similar results?

Ideally, I'd want the output generated to be

X is either 0 or 1
X is either 1 or 2

Thanks
Why does each case have 1 test that is the same as another case? Shouldn't it be more like:
Code:
```    switch (x)
{
case 0:
case 1:
cout << "X is either 0 or 1" << endl;
break;
case 2:
case 3:
cout << "X is either 2 or 3" << endl;
break;
}``` 12. The hypothetical switch you began with wouldn't print two messages either.

For this particular example, why not the following:
Code:
```#include <iostream>

int main()
{
int x = 1;
if (x > 0 && x <= 3) std::cout << "X is either " << x-1 << " or " << x << '\n';
if (x >= 0 && x < 3) std::cout << "X is either " << x << " or " << x+1 << '\n';
}``` 13. Originally Posted by Shaun32887 Yeah, you're right. I was thinking of another less efficient way to write the if statements.

Still, I would have the same problem.

Code:
```#include <iostream>
using namespace std;

int main( )
{
int x = 1;

if (x == 0 ||x == 1)
cout << "X is either 0 or 1" << endl;
else if (x == 1 ||x == 2)
cout << "X is either 1 or 2" << endl;
else if (x == 2 ||x == 3)
cout << "X is either 2 or 3" << endl;
system("pause");
return 0;
}```
This would still only display

"X is either 0 or 1"

Since it won't execute ALL the satisfied branches, just the 1st satisfied branch.

I think the conditional fallthrough might be my solution.
Remove the two 'else' statements and just use the 'if' and it should be what you want. 14. Originally Posted by anon The hypothetical switch you began with wouldn't print two messages either.
I know that, that's why I came here to ask the question.

I should also add that the point of the program wasn't to display those statements. Like I said, it was only a toy program, the concept was hitting two cases in one pass. 15. Originally Posted by Caverage Remove the two 'else' statements and just use the 'if' and it should be what you want.
Yeah... that's gonna work. Not sure why I didn't think of it myself, but that definitely works.

Thanks! Popular pages Recent additions 