+ 2

Why use if else instead of switch case

5th May 2019, 2:32 PM
Shubham Chaudhari
Shubham Chaudhari - avatar
6 Respostas
+ 5
IMHO choose `if` when you work with ranges of value, or need to specify multiple conditions to evaluate. `switch` works with finite value/expression, you don't (casually) pass multiple conditions to evaluate using `switch`, nor do you specify ranges to check, especially if the range was wide. Note that some languages do support ranges in `switch` cases, but the majority of languages don't.
5th May 2019, 2:59 PM
Ipang
+ 3
In Python you have no choice as in Python there is no switch statement.
5th May 2019, 3:25 PM
Hubert Dudek
Hubert Dudek - avatar
+ 1
Well there is no special reason to do one thing or another thing. You can both, so choice your favorite way to do that 🙄
5th May 2019, 2:45 PM
Werg Serium
Werg Serium - avatar
+ 1
Switch is better imo, it provides good readability and also more efficient than if-else,when you have a large set of values to compare
5th May 2019, 3:11 PM
kiRA
kiRA - avatar
+ 1
when performing many operations on sets of values, switch cases are the way to go. Using too many ifs can take up lots of memory. Either way, however, it doesnt matter to much as they both get the job done.
6th May 2019, 3:36 AM
Choe
Choe - avatar
+ 1
based on the condition in your code. I suggest use switch when there is lots of if condition as it will make code more readable than if.
15th Jun 2019, 4:14 PM
Vaibhav Raut
Vaibhav Raut - avatar