What is different between if else ladder and switch condition? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is different between if else ladder and switch condition?

15th Sep 2018, 5:21 PM
Rehan ALI
Rehan ALI - avatar
7 Answers
+ 4
In the condition of if statement, you can put an expression and compare it, or you can compare values, or you can use logical operators to combine two or more conditions into one. On the other hand you cannot do this in the switch statement. You can just put the variable in the condition of switch statement and make cases for that particular variable.
16th Sep 2018, 4:47 AM
blACk sh4d0w
blACk sh4d0w - avatar
0
answers frndz...
15th Sep 2018, 5:22 PM
Rehan ALI
Rehan ALI - avatar
0
The syntax 😅 In my opinion switch statements improve the readability of code. However some languages like python don't even have a switch command. Maybe depending on the task the one or the other might also be faster, but just a guess.
15th Sep 2018, 5:42 PM
Matthias
Matthias - avatar
0
I think it can depend on the language, but generally if/else is more flexible. You can have complex Boolean expressions in them, which isn't suitable for switch statements. A switch will compare on a value or enumerated type, but you could compare multiple types and conditions in if/else, so the readability can somewhat depend on the use case.
15th Sep 2018, 7:08 PM
Dan Walker
Dan Walker - avatar
0
with "if" you can achieve the same results as with switch, but it can be useful in some cases. I recomend reading this post, where the post creator asked for practical use of switch statements https://www.sololearn.com/discuss/1476353/?ref=app
15th Sep 2018, 8:38 PM
Data
Data - avatar
0
but both have major differences
20th Sep 2018, 10:28 AM
Rehan ALI
Rehan ALI - avatar
0
if else can take any data type for conditions but switch support all data type except double,float
20th Sep 2018, 10:30 AM
Rehan ALI
Rehan ALI - avatar