20

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:
Paul Michaels
  • 16,185
  • 43
  • 146
  • 269

10 Answers10

31

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
  • 15,403
  • 1
  • 27
  • 41
  • 2
    +1 for the second example. The first one makes me want to hurt someone. :) – Bobby Cannon Mar 31 '10 at 12:13
  • The first example seems strange: I know that it's true for, e.g., Java, but the [MSDN entry](https://msdn.microsoft.com/en-us/library/06tc147t.aspx) for `switch` explicitly states that the compiler won’t allow for “fall-throughs”. Or am I missing something here? – Informagic Jul 01 '16 at 12:13
  • Okay, apparently this is allowed for "empty" `case`s. – Informagic Jul 01 '16 at 13:36
8

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
  • 181,030
  • 38
  • 327
  • 365
  • already mentioned above, but important point about formatting :) I care about communication – Mike M Dec 08 '17 at 04:28
7

Whilst this wasn't possible when I originally asked this question, through the miracle of C# Pattern Matching, it now is (in C# 7):

switch (i)
{
    case var test when test <= 2:
        Console.WriteLine("Less than 2");
        break;

    case var test when test > 2 && test < 10:
        Console.WriteLine("Between 2 and 10");
        break;

    case var test when test >= 10:
        Console.WriteLine("10 or more");
        break;
}

A blog post on the subject

Paul Michaels
  • 16,185
  • 43
  • 146
  • 269
3

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.

Tomas Vana
  • 18,317
  • 9
  • 53
  • 64
3

One possibility is to convert your ranges into integers. For example:

//assuming x>=9 or if (x <= 0) return;
        switch((x+12)/7)
        {   case 1:Console.WriteLine("one");
                break;
            case 2:Console.WriteLine("2 through 8 inclusive");
                break;
            case 3:Console.WriteLine("9 through 15 inclusive");
                break;
            default: Console.WriteLine("16 or more");
                break;
        }
Peter Baum
  • 33
  • 5
2

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

leppie
  • 115,091
  • 17
  • 196
  • 297
1

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

Samuel DR
  • 1,215
  • 14
  • 28
-1

you can do

case 2:
case 3:
case 4:
...
case 8:
 // code here
 break
munissor
  • 3,755
  • 22
  • 24
-1

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
  • 55,302
  • 10
  • 87
  • 77
-1

You cannot use any conditional statements in a switch case.

If you want to execute the same lines of code for different options then you can do one thing:

switch (i)
            {
                case 0:
                case 1:
                case 2: 
                case 3:
                    //do something here.
                    break;
                default:
                    //do something here.
                    break;
            }
guntbert
  • 536
  • 6
  • 19
Hiscal
  • 633
  • 2
  • 8
  • 14