Any rule of thumb for using switch vs if-else for performance reasons? Basically like let's say if you need to use switch case if there are more than 4 conditions? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Any rule of thumb for using switch vs if-else for performance reasons? Basically like let's say if you need to use switch case if there are more than 4 conditions?

17th Apr 2016, 3:21 AM
Pratul Kumar Singh
Pratul Kumar Singh - avatar
2 Answers
+ 3
It seems that the compiler is better in optimizing a switch-statement than an if-statement. Check this out: http://www.blackwasp.co.uk/SpeedTestIfElseSwitch.aspx
17th May 2016, 2:01 PM
James Flanders
0
Switch Case has better efficiency and is faster and it uses a jump table method to reach the required block to execute. If-else uses different technique! In "if" statement, we can easily provide a range condition (for example: age>=18 && age<=45). There is no such option when using switch case: We can only provide a particular value while using switch case. Both have their own importance in different types of programs.
17th Jul 2016, 1:20 PM
Vibhu Agarwal
Vibhu Agarwal - avatar