5

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!")

Code Jockey
  • 6,611
  • 6
  • 33
  • 45
  • Your first code will not compile. Your second code is syntactically correct. A switch statement is a branch table (check out wikipedia). – DwB Aug 11 '11 at 21:07

4 Answers4

5

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.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • 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 – Code Jockey Aug 12 '11 at 16:15
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.

Geoff
  • 3,129
  • 23
  • 11
0

How About this? NOTE: I don't know that much Java, only Swift 2

var someIntegerValue = 1

func someSwitch(){

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

where you have a two button actions,

some action button NEXT

someIntegerValue +=1 // changes someIntegerValue to next case
someIntegerValue() //loads switch

some action button 2 Back

someIntegerValue -=1 // changes someIntegerValue to next case
someIntegerValue() //loads switch
dnaatwork.com
  • 362
  • 5
  • 9
0

You won't be able to do it. You can't have duplicate cases, the compiler will throw a hissy fit lol. I understand your logic in that you would like to check each case and then continue on and check other cases, but the break statement would pull you out of the switch-case statement anyways. What I would recommend is that you consider using a loop (i.e. for, while) if you want to check different things continuously. Coupling if statements with a loop will help you to execute bits of code before others.

Or if you really like the switch statements, you can create multiple switch-cases so that certain things happen before others.

HJM
  • 236
  • 1
  • 5
  • 16