If Statement vs. Switch Statement

in #programming7 years ago (edited)

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.