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?