What's the real difference between switch and else if? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

16th Jun 2016, 6:31 PM
Cristina
Cristina - avatar
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.
16th Jun 2016, 8:11 PM
Jenish Zinzuvadiya
Jenish Zinzuvadiya - avatar
+ 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.
17th Jun 2016, 6:31 PM
Sumit Balia
Sumit Balia - avatar
+ 1
Is only for better understanding...
16th Jun 2016, 7:46 PM
Lenin Jose Meza Zarco
Lenin Jose Meza Zarco - avatar
+ 1
As I read switch is like masked goto list, and else if is just ifs
1st Jul 2016, 9:04 PM
Azriel Gridfen
Azriel Gridfen - avatar
+ 1
switch statements offers better performance compared to if else statements ......
26th Nov 2016, 12:06 PM
Madhu Jayarama
Madhu Jayarama - avatar
0
Представь ситуацию что ты получаешь кодовоые значения клавишь клавиатуры, и при клике на каждую из клавишь будет выполнятся какое либо действие, конструкция else if в таком случаее будет очень большой и сложной, когда swich case будет занимать меншь места и проще в понимании кода
2nd Aug 2016, 5:48 PM
Ann Chebaeva
Ann Chebaeva - avatar
0
there is not difference
23rd Aug 2016, 2:55 PM
sagir
sagir - avatar
- 1
u need to use break in switch..
16th Jun 2016, 10:02 PM
SwimMing
SwimMing - avatar