views:

207

answers:

9

Does anyone know if it's possible to include a range in a switch statement (and if so, how)?

For example:

switch (x)
{
   case 1:
     //do something
     break;
   case 2..8:
     //do something else
     break;
   default:
     break;
}

The compiler doesn't seem to like this kind of syntax - neither does it like:

case <= 8:
+3  A: 

Short answer : no. It would be possible to write all of the cases there but such a range notation is not supported.

I think you have to use if statement here or switch to a language where there is a better support for case descrimination.

Thomas Wanner
+2  A: 

If you have so few cases, if would be much preferred.

leppie
+1  A: 

You could, handle the explicit cases case by case, and if you only have one range, deal with it in the default case.

Sdry
A: 

Longer answer: no, but if its for just a few options you can use fall-through:

switch (x)
{
   case 1:
   case 2:
   case 3:
   case 4:
     //do something else
     break;
   default:
     break;
}
Fabian
A: 

you can do

case 2:
case 3:
case 4:
...
case 8:
 // code here
 break
munissor
A: 

No, but you can write this, so you at least avoid writing the // do something else part multiple times.

switch (x)
{
   case 1:
     //do something
     break;
   case 2: case 3: case 4: case 5: case 6: case 7: case 8:
     //do something else
     break;
   default:
     break;
}
Marcelo Cantos
+8  A: 

No, this isn't possible. There are a few ways I've done this in the past:

Fixed coding:

switch (x)
{
   case 1:
     //do something
     break;
   case 2:
   case 3:
   case 4:
   case 5:
   case 6:
   case 7:
   case 8:
     //do something else
     break;
   default:
     break;
}

In combination with an if {} statement:

switch (x)
{
   case 1:
     //do something
     break;
   default:
     if (x <= 8)
     {
        // do something
     }
     else
     {
       // throw exception
     }
     break;
}
Andy Shellam
why the downvote? These are 2 perfectly acceptable solutions, unless the downvoter can tell me why they thought otherwise?
Andy Shellam
I really think people should have to add a comment as to WHY they downvote in order to be able to downvote. Pretty frustrating... :-/
Jaxidian
+1 to counter ridiculous downvote.
Dan Tao
Thanks Dan and Jaxidian, completely agree - I never downvote without commenting as to why.
Andy Shellam
+1 for the second example. The first one makes me want to hurt someone. :)
Bobby Cannon
A: 

You can use case fall through:

switch (x)
{
   case 1:
     //do something
     break;
   case 2:
   case 3:
   case 4:
   case 5:
   case 6:
   case 7:
   case 8:
     //do something else
     break;
   default:
     break;
}

But I'd just use if for this.

reko_t
A: 

Hi,

you can not used any conditional statement is switch case but If you wish to execute same line of code for different option than you can do one thing

switch (i)
            {
                case 0:
                case 1:
                case 2: 
                case 3:
                    //do something here.
                    break;
                default:
                    //do something here.
                    break;
            }

Thanks, Abhishek

Hiscal