"if" with "else if" OR "switch" with "deafault" | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

"if" with "else if" OR "switch" with "deafault"

So what is the significant difference between using if and adding multiple else if's and else AND using switch with default (this one works just like else in if)?

5th Oct 2016, 12:56 PM
Tigran
Tigran - avatar
4 Answers
+ 4
"If" is good with 1 to 3 options. when you have more then 3 options use switch.
19th Dec 2016, 7:40 AM
Bravekat
+ 3
Practically just clarity of the code. Technically "if" constructs are meant to be used also in more complex scenarios. You might have also performance differences, as the IL code is also different. The optimizer is able to recognize the similarity in simple cases though, and you could get really similar optimized code, but that stands only for really simple cases.
6th Oct 2016, 6:17 AM
Zoltán Zörgő
Zoltán Zörgő - avatar
+ 1
if else checks the trueness of any condition u can use as many if else if you used it inside a loop but in case of switch statement you have fixed amount of test case you need to check . So I guess if else is better when u have to deal with many condition.
1st Jan 2017, 6:50 AM
Harsh
- 1
If with else if is totally better
25th Oct 2016, 2:26 PM
UnknownCayon