+ 5

What is the difference between if else and switch?

4th Sep 2016, 5:23 PM
Omar Serrar
11 Answers
+ 11
The code is run through every 'else if' statement till a true is found, this is slow, and readability is poor. In 'switch', the control jumps to the corresponding case depending on value of the switch, and is more readable. Variable data type that can be used for 'else if' is both int and char, but whereas for 'switch' it can be int only.
4th Sep 2016, 6:49 PM
Umakanth V S
Umakanth V S - avatar
+ 3
with if else you can include as many statement needed.,but with switch numbers are identified within and also followed by the comparison value and a colon.
21st Sep 2016, 8:33 AM
Kyei Moses
Kyei Moses - avatar
+ 3
Use switch if you have int, double or char data types. For other types, switch doesn't work. For strings, use if
25th Jan 2017, 5:37 PM
Sayantan Chaudhuri
Sayantan Chaudhuri - avatar
+ 1
the if-else make 2 decision to run your command, except you using nested if-else... it mean if condition is true. then run a, if it's not then run b. but switch could give you a lot option which is it using number. if the option = 1 then run 1; , if the option = 100 then run 100; . if the option > 100 then run Your notification command that there is no option like that.
8th Sep 2016, 9:00 PM
Adhitya Yosua Sanjaya Andaria
Adhitya Yosua Sanjaya Andaria - avatar
+ 1
switch case only works for numeric types (int, double, float, short, byte, etc.) whereas if else works on all variable types. Switch case also allows for multiple "cases" in one statement, where if statements would require multiple statements. I love switch case, but many of my friends hate it, because they say it can be complicated to use at times
27th Feb 2017, 5:22 AM
Kyle Allevato
Kyle Allevato - avatar
0
You can use the switch case instead of using if else ladder. This is the advantage over using if else. There is no specific difference, just you need to understand where to use which.
4th Sep 2016, 5:30 PM
Sameer Kulkarni
Sameer Kulkarni - avatar
0
it is better to use switch when you have muliple choices
8th Sep 2016, 4:18 AM
abd alkader alnazer
abd alkader alnazer - avatar
0
The biggest difference its that the switch, in situations where you have too many possibities will let the code by far more ordered and easy to read/understand too... in my opinion its more efficient too in this case if we use a switch instead of an if.. think about the how many else statements that we should put for show a menu... Sorry for the english, greetings from Argentine
15th Oct 2016, 11:28 PM
Tomas Damianovich Reddy
Tomas Damianovich Reddy - avatar
0
switch is more flexible
19th Dec 2016, 3:20 PM
Seyedjalal Hesami
Seyedjalal Hesami - avatar
0
Switch is good when dealing with huge number of statements
23rd Jan 2017, 8:47 PM
Serge
Serge - avatar
0
lol everyone else stole my answer
18th Jul 2017, 8:16 PM
SuperCoder13
SuperCoder13 - avatar