[c++] Grouping switch statement cases together?

I may be over looking something but is there a simple way in C++ to group cases together instead of writing them out individually? I remember in basic I could just do:

SELECT CASE Answer
CASE 1, 2, 3, 4

Example in C++ (For those that need it):

#include <iostream.h>
#include <stdio.h>
int main()
{
   int Answer;
   cout << "How many cars do you have?";
   cin >> Answer;
   switch (Answer)                                      
      {
      case 1:
      case 2:
      case 3:
      case 4:
         cout << "You need more cars. ";
         break;                                        
      case 5:
      case 6:
      case 7:
      case 8:
         cout << "Now you need a house. ";
         break;                                        
      default:
         cout << "What are you? A peace-loving hippie freak? ";
      }
      cout << "\nPress ENTER to continue... " << endl;
      getchar();
      return 0;
}

This question is related to c++ switch-statement

The answer is


No, unless you want to break compatibility and your compiler supports it.


#include <stdio.h>
int n = 2;
int main()
{
     switch(n)
     {
          case 0: goto _4;break;
          case 1: goto _4;break;
          case 2: goto _4;break;
          case 3: goto _4;break;
          case 4:
              _4:
                printf("Funny and easy!\n");
                break;
          default:
                printf("Search on StackOverflow!\n");
                break;
     }
}

Sure you can.

You can use case x ... y for the range

Example:

#include <iostream.h>
#include <stdio.h>
int main()
{
   int Answer;
   cout << "How many cars do you have?";
   cin >> Answer;
   switch (Answer)                                      
      {
      case 1 ... 4:
         cout << "You need more cars. ";
         break;                                        
      case 5 ... 8:
         cout << "Now you need a house. ";
         break;                                        
      default:
         cout << "What are you? A peace-loving hippie freak? ";
      }
      cout << "\nPress ENTER to continue... " << endl;
      getchar();
      return 0;
}

Make sure you have "-std=c++0x" flag enabled within your compiler


Your example is as concise as it gets with the switch construct.


You can't remove keyword case. But your example can be written shorter like this:

switch ((Answer - 1) / 4)                                      
{
   case 0:
      cout << "You need more cars.";
      break;                                        
   case 1:
      cout << "Now you need a house.";
      break;                                        
   default:
      cout << "What are you? A peace-loving hippie freak?";
}

AFAIK all you can do is omit the returns to make things more compact in C++:

switch(Answer)
{
    case 1: case 2: case 3: case 4:
        cout << "You need more cars.";
        break;
    ...
}

(You could remove the other returns as well, of course.)


gcc has a so-called "case range" extension:

http://gcc.gnu.org/onlinedocs/gcc-4.2.4/gcc/Case-Ranges.html#Case-Ranges

I used to use this when I was only using gcc. Not much to say about it really -- it does sort of what you want, though only for ranges of values.

The biggest problem with this is that only gcc supports it; this may or may not be a problem for you.

(I suspect that for your example an if statement would be a more natural fit.)


If you're willing to go the way of the preprocessor abuse, Boost.Preprocessor can help you.

    #include <boost/preprocessor/seq/for_each.hpp>

    #define CASE_case(ign, ign2, n) case n:

    #define CASES(seq) \
        BOOST_PP_SEQ_FOR_EACH(CASE_case, ~, seq)

    CASES((1)(3)(15)(13))

Running this through gcc with -E -P to only run the preprocessor, the expansion of CASES gives:

    case 1: case 3: case 15: case 13:

Note that this probably wouldn't pass a code review (wouldn't where I work!) so I recommend it be constrained to personal use.

It should also be possible to create a CASE_RANGE(1,5) macro to expand to

    case 1: case 2: case 3: case 4: case 5:

for you as well.


You can use like this:

case 4: case 2:
 {
   //code ...
 }

For use 4 or 2 switch case.