Unfortunately, until now we still can't use CASE yet. It is reserved but not yet implemented. You can use the method in the link, using map object. Or for CASE alternative, it is actually a series of if-else.
Code: Select all
fruit = "apple";
msg = "You have selected ";
if(fruit == "apple")
{
msg = msg + "red apple";
}
else if(fruit == "banana")
{
msg = msg + "yellow banana";
}
else if(fruit == "cherry")
{
msg = msg + "red cherry";
}
else
{
msg = "You don't select any of the fruit";
}
Yeah, there is no shortcut to do this now. You can remove the else, but that will make each if() not mutually exclusive anymore. Means that several if() can be executed if the condition is true for the case. Using else will make sure only one of the if() get executed, not the other.
I actually have the similar usage case, and I use several if() but not with else. As I have make sure each condition is unique, so I don't put else to protect them. The execution speed is the same.