HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpCritical

Multiple cases in switch statement

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
switchmultiplestatementcases

Problem

Is there a way to fall through multiple case statements without stating case value: repeatedly?

I know this works:

switch (value)
{
   case 1:
   case 2:
   case 3:
      // Do some stuff
      break;
   case 4:
   case 5:
   case 6:
      // Do some different stuff
      break;
   default:
       // Default stuff
      break;
}


but I'd like to do something like this:

switch (value)
{
   case 1,2,3:
      // Do something
      break;
   case 4,5,6:
      // Do something
      break;
   default:
      // Do the Default
      break;
}


Is this syntax I'm thinking of from a different language, or am I missing something?

Solution

There is no syntax in C++ nor C# for the second method you mentioned.

There's nothing wrong with your first method. If however you have very big ranges, just use a series of if statements.

Context

Stack Overflow Q#68578, score: 374

Revisions (0)

No revisions yet.