Switch statements vs. if statements | SoloLearn: Learn to code for FREE!


Switch statements vs. if statements

so switch statements do the same thing as if statements that only use the equality operator as the test?

7/8/2016 5:10:06 AM

Julie Kang

3 Answers

New Answer


Hi Julie, you are right. It's basically a list of simple equality tests on one variable. Ayush, I think you might have overshot a little making it too simple. The switch-statement only allows branching on a single variable without negation (you can't do "not" in the switch-statement). The if-statement can do *much* more, e.g. not just equals expressions as *all* comparison operators are available (not, greater than etc.) and you can therefore also express a lot more.


Wouldn't using multiple if statements be the same as a switch except the switch requires less typing.


Adam, you are right. Unfortunately, Ayush removed his answer I was referring to. In his answer he basically stated that the switch statement is a "simple" version of the list if statements. This still hold true, so there's a good reason for the switch statement to exist: it simplicity helps to understand what you want to express.