Multiple / Repeating Cases in a Java Operator Expression

I would like to know how Java handles several 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 piece of code executed for case 1 or 2 (using dips), but then later so that the code snippet is executed only for case 2.

Rather, the following is needed instead:

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

My actual code is more complex, but it will use the same principle (for example, 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 : everything is done! ")

+4
source share
4 answers

You cannot repeat cases in a Java statement, this is a compilation error. You will need to do what you suggested, which actually looks like good factoring.

+1
source

Is there something wrong with the 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; } 

What would i do.

+5
source

You cannot do it. You cannot have duplicated cases, the compiler will throw hiss. I understand your logic in that you want to check each case, and then continue and check other cases, but the break statement will pull you out of the case-switch statement anyway. I would recommend that you consider using a loop (i.e., For, while) if you want to constantly check for different things. Chaining if loop statements help you execute a bit of code in front of others.

Or, if you really like switch instructions, you can create several wiring closets so that some things happen before others.

0
source

How about this? NOTE. I do not know that a lot of 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 actions with two buttons,

NEXT action button

 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 
0
source

All Articles