+ 5
What is the difference between if else and switch?
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.
+ 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.
+ 3
Use switch if you have int, double or char data types. For other types, switch doesn't work. For strings, use if
+ 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.
+ 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
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.
0
it is better to use switch when you have muliple choices
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
0
switch is more flexible
0
Switch is good when dealing with huge number of statements
0
lol everyone else stole my answer