# Thread: switch statements with greater/less than operators

1. ## switch statements with greater/less than operators

How would I create a switch statement that would have cases such as 'x < r' or something like that if x is the switch parameter?

Something like-
Code:
```char x=10;
switch(x)
{
case > 1: cout << "x is greater than 1"; break;
case < 1: cout << "x is less than 1"; break;
default: cout << "Other..."; break;
}```
?

2. >>How would I create a switch statement that would have cases such as 'x < r' or something like that if x is the switch parameter?
You can't, switch cases have to be integral literals. Use an if/else series instead :-)

EDIT:
Or you can use a true/false switch if that's all you want
Code:
```switch (x > 1)
{
case true: // Something
case false: // Something else
}```

3. I don't think the true/false statement would work in my case. Just in my sloppy code if/else if statements just make the code look that much worse.

4. Using the ternary operator you could do a one-liner and perhaps not sacrifice the purtiness of your code...

Code:
`(x > 1 ? cout << "greater that one" : cout << "less than one");`

5. You could expand on both replies

Code:
```#include <iostream>

using namespace std; //introduces namespace std

int main ( void )
{
int x=5, y=8;

switch(x > y ? true : false) // or 1 : 0
{
case true:  cout << x
<< " is greater than "
<< y;

break;

case false: cout << y
<< " is greater than "
<< x;

break;
};

return 0;
}```

6. >>switch(x > y ? true : false) // or 1 : 0
Why do that? Using a relational operator automatically returns true and false, it's redundant to do it explicitly :-)

7. Dunno... my mind wanders at times since I'm going back and forth between this board, researching for a paper, and checking E-mail. :P

8. ## ~

"How would I create a switch statement that would have cases such as 'x < r' or something like that if x is the switch parameter?"

Code:
```if (x < r) {

}```
Functionality over visuality.