Multiple/repeating cases in a Java switch statement

12,628

Solution 1

You cannot repeat cases in a Java switch statement, it is a compile error. You will need to do as you have suggested, which actually looks like a good factoring.

Solution 2

Anything wrong with two switch statements?

switch (someIntegerValue) {
   case 1:
   case 2:
      DoSomethingForBothCases();
      break;
   case 3:
      DoSomethingUnrelated();
      break;
}

switch (someIntegerValue) {
   case 1:
      DoSomethingForCase1ThatReliesUponExecutionOfTheEarlierFunctionCall();
      break;
   case 2:
      DoSomethingForCase2ThatReliesUponExecutionOfTheEarlierFunctionCall();
      break;
}

That's what I would do.

Share:
12,628
Code Jockey
Author by

Code Jockey

I find it ridiculous that in the hunt for reputation people who have their answers down-voted with an explanation as to why will hunt down a previous answer of the person that explained why they down-voted and subsequently down-vote that answer just for spite. I mean seriously? Relax.

Updated on June 04, 2022

Comments

  • Code Jockey
    Code Jockey almost 2 years

    I would like to know how Java handles multiple identical instances of the same case. I think the following makes sense, conceptually:

    switch (someIntegerValue)
    {
       case 1:
       case 2:
          DoSomethingForBothCases();
          break;
       case 3:
          DoSomethingUnrelated();
          break;
       case 1:
          DoSomethingForCase1ThatReliesUponExecutionOfTheEarlierFunctionCall();
          break;
       case 2:
          DoSomethingForCase2ThatReliesUponExecutionOfTheEarlierFunctionCall();
          break;
    }
    

    Essentially, I would like to have a chunk of code executed for either case 1 or 2 (using fall-through), but then later on, have a chunk of code only executed for case 2.

    Rather, is the following necessary, instead?

    switch (someIntegerValue)
    {
       case 1:
          DoSomethingForBothCases();
          DoSomethingForCase1ThatReliesUponExecutionOfTheEarlierFunctionCall();
          break;
       case 2:
          DoSomethingForBothCases();
          DoSomethingForCase2ThatReliesUponExecutionOfTheEarlierFunctionCall();
          break;
       case 3:
          DoSomethingUnrelated();
          break;
    }
    

    My actual code is more complex, but would use the same principle (i.e. something like "case 1: nope; alright... case 2: yep! execute this code!; case 3: nope; case 1 again?: still nope!; case 2 again?: yep! execute this code; no more cases: All Done!")

  • TJunkie
    TJunkie over 12 years
    with Geoff on this - the second example in the OP looks good.
  • Code Jockey
    Code Jockey over 12 years
    nothing wrong in particular, (and if case 3 was not completely unrelated, then two switch statements might be needed) but I would prefer to use the second code chunk in my OP over two switches. It comes down to style, because the compiled code (at least in a fairly simple case like this) won't be significantly more or less efficient either way! :D
  • dnaatwork.com
    dnaatwork.com almost 8 years
    Also don't forget to load your function in the beginning.