If Statement vs. Switch Statement

Words
88
Reading
1 min
Listen
Play
9y

If Statement vs. Switch Statement

If Statement


ifstatement.png

Switch Statement


switch.png

In my experience switch statement is faster than if statement. It always depends on the situation whether you will use if statement or switch statement. I use switch statement when I am trying to switch the value of a single variable. However switch statement is not always faster than the if statement as some of the compilers are able to optimize performance by constructing jump tables. In this way each condition doesn't have to be tested in if/else statements.

If Statement vs. Switch Statement | Ecency