0

Switch statements vs. if statements

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

8th Jul 2016, 5:10 AM
Julie Kang
Julie Kang - avatar
4 Antworten
+ 3
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.
8th Jul 2016, 10:19 AM
Stefan
Stefan - avatar
0
Wouldn't using multiple if statements be the same as a switch except the switch requires less typing.
15th Jul 2016, 7:02 PM
Adam
0
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.
15th Jul 2016, 8:39 PM
Stefan
Stefan - avatar
0
i need to create a program in switch statements and its very hard for me pls help me
4th Nov 2020, 1:19 PM
camille garcia
camille garcia - avatar