+ 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)?
4 Réponses
+ 4
"If" is good with 1 to 3 options. when you have more then 3 options use switch.
+ 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.
+ 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.
- 1
If with else if is totally better