+ 2
When do i use switch?
i don't get it really, over what kind of situation do i need to apply it?
4 Answers
+ 7
I usually wait until I've got more than 3 choices of my variable.
if (x == 0)
;//do something
else if (x == 1)
;//do something
else if (x == 2)
;//do something
else
;//do something
I'd use a switch for this. It use to be that using a switch ended up generating better code than the if code would get. However, the odds are good that today's compilers will automatically switch your if or switch to use which ever has the better code so it doesn't matter.
+ 3
@John Wells
This person is asking in java...
And despite that you answered a âelse ifâ clause.
+ 1
syul what I coded compiles and runs in Java.
+ 1
i actually get it now, it's basically the same function as an if