Why use if else instead of switch case | SoloLearn: Learn to code for FREE!

+2

Why use if else instead of switch case

5/5/2019 2:32:01 PM

Shubham Chaudhari

6 Answers

New Answer

+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.

+3

In Python you have no choice as in Python there is no switch statement.

+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 🙄

+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

+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.

+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.