3

I would like to do something like this:

int i = 0;

switch(difficulty) {
    case 1: i++; break;
    case 2: i--; break;
    default: case 1;
}   

Is something like this possible? I want to prevent duplicate code. I know that in this particular example there is little reason to do this, since the duplicated code would be small. The only thing I can come up with is something like this [using the fall through abilities of switch cases]:

switch(difficulty) {
    case 2: i--; break;
    default:
    case 1: i++; break;
}   

I would rather not do it like this, since it would make more sense to increase case numbers and have the default at the bottom.

But I wonder that if I would do this, would it mess up with the goto statements under the hood? In particular, will it not take longer to decide which goto statement to use since the numbers or out of order? Does the order matter in the switch statement? Imagine all cases have the same odds of being called, would it matter if you had them in random order instead of linear order?

[Edit: for my side question about efficiency I found this: Does the order matter of switch-statements, the short answer is no: Does switch case order affect speed? How does Java's switch work under the hood?

Community
  • 1
  • 1
Joop
  • 3,706
  • 34
  • 55
  • 4
    Why not remove case 1: and just leave the default? Default means all other values - including 1 if not defined elsewhere. – BarrySW19 Nov 24 '14 at 16:07
  • 2
    Add a private method and call it from both `case 1` and `default`, that way you won't have duplicated code. – Predrag Maric Nov 24 '14 at 16:11
  • @LutzHorn `goto` is like any tool; misused, it's harmful; used correctly, it is immensely powerful. And yes, I miss `goto` in Java some times. Rarely, but some times. – fge Nov 24 '14 at 16:14
  • @PredragMaric that would be a good alternative also! Why not make it a short answer? – Joop Nov 24 '14 at 16:22
  • @Joop Sure, added an answer. – Predrag Maric Nov 24 '14 at 16:25

5 Answers5

10

This should suit your needs:

switch(difficulty) {
    case 2: i--; break;
    default: i++; break;
}
sp00m
  • 47,968
  • 31
  • 142
  • 252
  • This is not what I was trying to find out. Imagine you have 3 levels, 3 cases for each. And you want to keep the linear order of the levels, since that would make logical sense. Can you do something like this without changing the linear order? – Joop Nov 24 '14 at 16:10
4

This is probably what you need/want, and is valid code

int i = 0;      
    switch (difficulty) {
    default:
    case 1: i++; break;
    case 2: i--; break;
    }
Kennet
  • 5,736
  • 2
  • 25
  • 24
  • I think this must be the best way to do what I want. Only the place where the default is located is changed and not the cases. It would be even better if I could keep the default at the bottom, but this trade-off is better than re-ordering cases for which the order makes particular sense. Thanks! – Joop Nov 24 '14 at 16:18
3

As far as I know, the order of the cases does not alter in any way the switch statement, either your parameter is fit for a case or it isn't, so it doesn't matter, in your case, which case you have first. You can see the documentation as reference if you wish. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

1

(moved from comment, since it seemed to be helpful)

Add a private method and call it from both case 1 and default, that way you won't have duplicated code.

Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
0

No, in switch statement order doesn't matter at all. Also the compiler always maintain a hash table for jumping to the switch statement. So ordering or increase of casing is not a matter at all. It's always a O(1) operation. However, this will reduce the switching for your case:

switch(difficulty) {
    case 2: i--; break;
    default: i++; break;
}
xlecoustillier
  • 16,183
  • 14
  • 60
  • 85
Tanjeer
  • 89
  • 4
  • 2
    "*the compiler always maintain a hash table for jumping to the switch statement.*" => not always, no. – assylias Nov 24 '14 at 16:20
  • @Tanjeer - see https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-3.html#jvms-3.10 - the switch may be implemented either by indexing or searching through a table. – Andy Thomas Nov 24 '14 at 16:23
  • [This answer](http://stackoverflow.com/a/12938176/2991525) explains that your O(1)-claim is wrong. – fabian Nov 24 '14 at 16:29
  • Yeah, I admit. But it's not always wrong depending whether compiler uses table switch or lookup switch. Thanks for making me correct. – Tanjeer Nov 24 '14 at 16:40