'Is it possible to convert an `if` statement into a `switch` statement? [closed]

Is it possible to convert an if statement into a switch statement in java?



Solution 1:[1]

It depends on what is being evaluated in the if and how many of such related if statements you have.

If you have

if (i == 1)
{
    function1 (i)
}
else
if (i == 2)
{
    function2 (i)
}

if (i == 3)
{
    function3 (i)
}

then yes.

switch (i) {
    case 1:
        function1(i);
        break;
    case 2:
        function2(i);
        break;
    case 3:
        function3(i);
        break;
}

If you had multiple evaluations, then it becomes harder. if (i == 1) && (j == 2) will be much harder to represent in a switch-case block.

Solution 2:[2]

I would not convert a single if statement into a switch statement, however it can be done. You will need to specify a default for the switch. If you are using many if...else statements that would warrant the switch.

int month = 8;
String monthString = "";
if(month == 1)
{
   monthString= "January";
}else if(month == 2)
{
  monthString= "February";
}else if(month== 3)
{
  monthString= "March";
}
etc....

Could be wrote like:

int month = 8;
String monthString;
switch (month) {
    case 1:  monthString = "January";       break;
    case 2:  monthString = "February";      break;
    case 3:  monthString = "March";         break;
    case 4:  monthString = "April";         break;
    case 5:  monthString = "May";           break;
    case 6:  monthString = "June";          break;
    case 7:  monthString = "July";          break;
    case 8:  monthString = "August";        break;
    case 9:  monthString = "September";     break;
    case 10: monthString = "October";       break;
    case 11: monthString = "November";      break;
    case 12: monthString = "December";      break;
    default: monthString = "Invalid month"; break;
}

Solution 3:[3]

According to this example on oracles website, yes. Yes you can. Here's the link

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

It just depends on how much typing you want to do. You can have a switch statement that only has one condition, or if statements that handle multiple conditions.

Solution 4:[4]

In theory sure, but they are intended to check different things. An if statement is meant to respond to a particular boolean condition whereas a switch statement is meant to respond to a number of different possibilities. That said, you could easily rewrite a switch statement as a series of if else statements or vice versa. Typically though you really wouldn't want to do that. Use a hammer to drive nails and use a screw driver to drive screws. You could use either tool for either fastening device, they just don't work as well.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Raj More
Solution 2 Kevin Bowersox
Solution 3 canadiancreed
Solution 4 Chris Thompson