Is it possible to convert an if
statement into a switch
statement in java?
First of all, it's technically a switch block. Second of all, yes it is possible, but extremely unnecessary. However, if you want to do it, here is one way you could make the conversion...
int theCase;
if (someVariable.equals(someString)) {
theCase = 1;
} else if (someVariable.equals(someOtherString)) {
theCase = 2;
} else {
theCase = 3;
}
switch (theCase) {
case 1:
//some code here
break;
case 2:
//some code here
break;
case 3:
//some code here
break;
}
Of course, if you are comparing something other than strings you would use the ==
operator.
The two are also interchangeable, but only if you're working with chars
or ints
and only one condition is being tested.
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.
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;
}
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.
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.
精彩评论