2

I have a switch case in Java like this:

switch(int example)
{
   case 1: //Do different
      break;
   case 2: //Do different 
      break;
   /** For int more than 2, then I need
       for it to do something same.
   */
   case 3://Do different and case6
      break;
   case 4://Do different and case6
      break;
   case 5://Do different and case6
      break;
   case 6:
      break;
}

What is an elegant way to do this, sans having a special case 6 function that case 3-5 calls? (I use int here, but that is an example, so I can't use if(int >2))

Secret
  • 3,291
  • 3
  • 32
  • 50

5 Answers5

3

A switch can't really do exactly what you are asking out of the box. You can construct something like this with a nested switch though:

outer_switch: switch (example) {

    case 1: System.out.println("1");
            break;

    case 2: System.out.println("2");
            break;

    default: {
        switch (example) {

            case 3: System.out.println("3");
                    break;

            case 4: System.out.println("4");
                    break;

            case 5: System.out.println("5");
                    break;

            case 6: System.out.println("6");
                    break;

            default: break outer_switch;
        }

        System.out.println("not 1 nor 2");
    }
}

Note the labeled break on outer_switch which is a way to circumvent the shared code if example does not meet any of the inner cases.

Radiodef
  • 37,180
  • 14
  • 90
  • 125
2

One way I could think of is to move your code to different functions. Something like this.

void case1(){
    // do something
}
...
void case3(){
   // do something
    case6();
}
...
void case6(){
   // do something
}

// This switch is in some other method.
switch(int example)
{
   case 1: //Do different
      case1();
      break;
   ...
   case 3://Do different and case6
      case3(); //internally calls case 6
      break;
   ...
   case 6:
      case6();
      break;
}

Or you can even have different methods for each case and call case3() and case6() methods in the case 3:. Either way, the methods solutions would work, and IMHO, it would be a bit more elegant and multiple switch statements.

Rahul
  • 44,383
  • 11
  • 84
  • 103
1

I'm not sure if it's elegant, but one way would be to have two switch blocks:

switch(int example)
{
   case 1: //Do different
      break;
   case 2: //Do different 
      break;
   case 3:
      // Do whatever
      break;
   case 4:
      // Do whatever
      break;
   case 5:
      // Do whatever
      break;
}

switch(int example)
{
   case 3:
   case 4:
   case 5:
   case 6:
      // Do whatever (case 3-5 fall through)
      break;
}
devnull
  • 118,548
  • 33
  • 236
  • 227
  • case 3 shouldn't run case 4 nor 5, but 6 only. Thank you though. – Secret Nov 23 '13 at 06:15
  • @Secret In the second `switch` statement, you aren't doing anything in case 3 to case 5. They simply make it fall through to case 6. – devnull Nov 23 '13 at 06:17
  • Oh yeah I was bit blind there. =) Radiodef's answer I feel is more compact, but this is the safest since we don't run default. – Secret Nov 23 '13 at 06:24
1

Although your code is not pretty it's probably going to give you decent performance. Your other obvious option is an if-elseif-else statement. See the accepted answer here for why switch might be the best option, and here to see what performance issues you might run into with large switch statements in Java.

Community
  • 1
  • 1
mttdbrd
  • 1,791
  • 12
  • 17
1

This might also be a solution to what you want to achieve:

       switch(example){
            case 1:
                System.out.println(example);
                break;
            case 2:
                System.out.println(example);
                break;
            case 3:
                System.out.println("I'm case 3");
            case 4:
                if (example == 4){
                    System.out.println("I'm case 4");
                }
            case 5:
                if (example == 5){
                    System.out.println("I'm case 5");
                }
            case 6:
                System.out.println("I'm in extra case " + example);
                break;
        }

The idea is that you add an extra condition check to let your code fall through all the branches without executing unnecessary ones.

svz
  • 4,516
  • 11
  • 40
  • 66