Multiple values in switch case / converting ifs to switch statement

17,960

Solution 1

You could do something like this, but YMMV according to possible exit conditions in your code:

switch (x) {
case 1: 
  ...
  break;
case 2:
  ...
  break;
case 3:
case 4:
  ... multi-case
  break;
default:
  if(myArray.contains(x)){
    ...
  }
}

Solution 2

Switch statement can be used only to test equality of x

So if not equality conditions like (if(myArray.contains(x))) must come at the end, then you can copy paste this into default section of switch

It would look this way

    switch (x) {
     case 1:   ...; break;
     case 2:   ...; break;
     default: if(myArray.contains(x)) ...
    }

If not equality conditions would need to be in the middle, then its not possible to use switch.

reference: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

Solution 3

You can only swith over the x value. That means the last condition will not be part of the switch:

switch(x) {
case 1:
    ...
    break;
case 2:
    ...
    break;
}
if(myArray.contains(x)){
    ...
}

Solution 4

Yes, it is possible. The switch statement will make it a little cleaner and easier to maintain. You have to worry about x because it has to be compile-time constant.

switch(x) {
 case 1:
    doSomething();
    break();
 case 2:
    doSomethingElse();
    break();

default:
 if(myArray.contains(x)){

 }


//etc...
}
Share:
17,960
Steve
Author by

Steve

Updated on June 04, 2022

Comments

  • Steve
    Steve almost 2 years

    I have a big list of Ifs, and I want to change it into a switch statement. Currently it looks like this:

    if(x == 1){
        ...
    }
    if(x == 2){
        ...
    }
    if(myArray.contains(x)){
        ...
    }
    

    In actuality it's a bit longer than this, but it's the third if in the example that confuses me - how do I change that around to get it to work in a switch, or is it even possible?

  • rds
    rds over 12 years
    This is also slightly different: switch/case is a if ... else if. In the siutation where x is modified in a "then" block statement, the overall behaviour will be different.
  • Anthony Grist
    Anthony Grist over 12 years
    Unlike the first two ifs in the question, the third can still be true even if one of the preceding ones is also true. x can't be equal to 1 AND 2 at the same time, but it can be equal to 1 and contained within myArray at the same time. Moving the if statement into the case statements default is likely not the correct solution.
  • Steve
    Steve over 12 years
    Sorry I should have clarified - the values in the array will never match anything else that I'm checking for, so it's not an issue. good catch though.
  • solendil
    solendil over 12 years
    You're both right, that's why I told YMMV... I assumed that the code in every if had an exit. Orelse the original code looks like a plate of spaghettis. Multiple ifs, each modifying the variable for itself and the next if, no elsif, a final condition that seems logically unrelated. The meaning of the original code is far from clear, so there's no straightforward translation for it. My answer provide a possible solution, but only the original poster will know if it suits his needs.