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.

Coin Marketplace

STEEM 0.16
TRX 0.15
JST 0.029
BTC 55747.11
ETH 2323.34
USDT 1.00
SBD 2.34