1

Am I able to add sets instead of numbers to "switch" statement? Like:

switch(number)
{
 case 1<50: //if number is between 1 and 50
 {
 blah;
 break;
 }
 case 50<100: //if number is between 50 and 100
 {
 blah;
 break;
 }

and so on.

Zoltán Schmidt
  • 1,286
  • 2
  • 28
  • 48

7 Answers7

6

(Edit following comments below, acknowledgement given to user syam)

No. In C++ you can only switch on things that reduce to an integer. You'll need to build a function that computes integral results for 1 < 50 and 50 < 100 etc. and use switch(thatFunction(...)).

Or, if you don't need the follow-though idiom that a switch gives you (by the looks of your example, you don't), just use if, else if, else.

Extract from below comments: 6.4.2-2 [stmt.switch] The condition shall be of integral type, enumeration type, or of a class type for which a single non-explicit conversion function to integral or enumeration type exists [...] the constant-expression shall be a converted constant expression (5.19) of the promoted type of the switch condition

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • _You'll need to build a function that computes int results for 1 < 50 and 50 < 100 etc. and use switch(thatFunction(...))._ can you explain it? It sounds useful. Also, I don't want to use `if` and `else` because I'd like to use it to watch some regions on the screen and it means the use of a lot of coordinates. – Zoltán Schmidt May 23 '13 at 12:48
  • @Bathsheba You can only switch on *integers* not just on `int`. `int64_t` and `char` are perfectly acceptable types. – syam May 23 '13 at 12:51
  • What is the exhaustive list of things you want to switch on? You have 1 < 50 and 50 < 100, but they are pretty meaningless as they are just integer literals. – Bathsheba May 23 '13 at 12:52
  • Hey syam: are you sure about that? I'm convinced (insofar that I grew up thinking) that in C any number larger than an int will introduce a periodicity in a switch. Is that not true in C++ too? – Bathsheba May 23 '13 at 12:53
  • @Bathsheba: *6.4.2-2 [stmt.switch] The condition shall be of integral type, enumeration type, or of a class type for which a single non-explicit conversion function to integral or enumeration type exists [...] the constant-expression shall be a converted constant expression (5.19) of the promoted type of the switch condition.* Or just try to switch on `4294967299LL` (2^32 + 3) and have a case with `3` -- assuming of course your `int` is 32 bits and `long long` is 64 bits. ;) – syam May 23 '13 at 13:04
  • Syam: If you put this as an answer I've give you +10. – Bathsheba May 23 '13 at 13:06
  • 1
    @Bathsheba: nah, just edit your answer to read *integer* instead of `int` and I'll be satisfied. ;) – syam May 23 '13 at 13:07
2

1 < 50 is a boolean expression that gets a compile-time value of true, and thus becomes 1 in an integer context. So you end up with two identical cases in your switch. Compile with high warning level - your compiler will surely complain.

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
1

Sadly not. You can use fall-through for a small set of discrete values:

case 1:
case 2:
    blah;
    break;

but for large ranges the only sensible option is if...else.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

For me, this works on my g++ (GCC) 4.7.2 20121109

#include <iostream>

using namespace std;

int main() {

    switch(6) {
        case 1 ... 5:
            cout << "Between 1 and 5" << endl;
            break;
        case 6 ... 10:
            cout << "Between 6 and 10" << endl;
            break;
    }
}

Thanks for stating in the comments that it's GCC's extenstion

Jan Vorcak
  • 19,261
  • 14
  • 54
  • 90
0

Conditionals are not permitted as case statements, so your option is to use if / else statements.

0

You can't do that in C++. case switches must be a constant that can be converted to an int.

Andrea Bergia
  • 5,502
  • 1
  • 23
  • 38
0

This is nicely discussed here.

A switch statement is useful when you have to use a single variable across many possible conditions. A switch statement works faster than an if-else block by using a jump table. It requires constant integers when it makes the table. The reason expressions that dont evaluate to a fixed value are not allowed is to save from ambiguity.

case 1+a;

break;

case 5;

break;

Now what if a is 4. That will cause issues. If it doesnt use the jump table it wont be much useful than an if-else block

Community
  • 1
  • 1
Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59