+ 2
What's the real difference between switch and else if?
If I understood well, both stop when they have a match and ignore the other cases. So why use one and not the other? It's only for better reading of the code? I've been told many times that switch is better when working with many options, but I dpn't really see a difference.
8 Answers
+ 6
switch provide less coding and high performance when you have plenty amount of conditions.
because else if check with all conditions until not true condition found. while switch switches to right condition directly may be.
+ 3
if you get result in middle of the loop so in switch you can break the loop but in while complete loop it's execute.
+ 1
Is only for better understanding...
+ 1
As I read switch is like masked goto list, and else if is just ifs
+ 1
switch statements offers better performance compared to if else statements ......
0
Представь ситуацию что ты получаешь кодовоые значения клавишь клавиатуры, и при клике на каждую из клавишь будет выполнятся какое либо действие, конструкция else if в таком случаее будет очень большой и сложной, когда swich case будет занимать меншь места и проще в понимании кода
0
there is not difference
- 1
u need to use break in switch..