Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}
like image 994
Josh Lake Avatar asked Dec 20 '10 21:12

Josh Lake


People also ask

How do you handle multiple cases on a switch?

The switch can includes multiple cases where each case represents a particular value. Code under particular case will be executed when case value is equal to the return value of switch expression. If none of the cases match with switch expression value then the default case will be executed.

Can you add two conditions switch case?

You can use have both CASE statements as follows. FALLTHROUGH: Another point of interest is the break statement. Each break statement terminates the enclosing switch statement.

Can you have multiple switch statements?

There can be any number of case statements within a switch.

Can switch cases be out of order?

A Switch is made up of a bunch of Cases and one will fire that matches a condition. The order technically does not matter.


3 Answers

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.)

like image 154
Leo Davidson Avatar answered Nov 03 '22 15:11

Leo Davidson


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

like image 41
Ankit Patel Avatar answered Nov 03 '22 15:11

Ankit Patel


No, but you can with an if-else if-else chain which achieves the same result:

if (answer >= 1 && answer <= 4)
  cout << "You need more cars.";
else if (answer <= 8)
  cout << "Now you need a house.";
else
  cout << "What are you? A peace-loving hippie freak?";

You may also want to handle the case of 0 cars and then also the unexpected case of a negative number of cars probably by throwing an exception.

PS: I've renamed Answer to answer as it's considered bad style to start variables with an uppercase letter.

As a side note, scripting languages such as Python allow for the nice if answer in [1, 2, 3, 4] syntax which is a flexible way of achieving what you want.

like image 12
moinudin Avatar answered Nov 03 '22 14:11

moinudin