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