Switch vs If-Else

Any suggestions to when using switch over if-else would be faster and why?

When there are few conditions(For ex. just 5-6 conditions) use if-else.
When there are more conditions, use switch.

2 Likes

If you need to use range (example from 1-10) you must use if, use switch for specific number/char

As all have said for small number of conditions (1-5) it doesn’t matter whichever you use.But for large number of conditions with switch cases in narrow range switch case is much faster than if-else.

for “Why switch is faster” ??

Compiler generates a jump table for a switch and instead of checking which case is satisfied, it only decides which case has to be executed.

Case values in narrow range

If the case values are placed in a narrow range, the compiler can avoid performing a comparison for every case leg in the switch statement. In such cases, the compiler generates a jump table which contains addresses of the actions to be taken on different legs. The value on which the switch is being performed is manipulated to convert it into an index into the jump table. In this implementation, the time taken in the switch statement is much less than the time taken in an equivalent if-else-if statement cascade. Also, the time taken in the switch statement is independent of the number of case legs in the switch statement.It then basically becomes O(1) time complexity.

*Case values in wide range

If the case legs of the switch statement have a wide deviation in values, the compiler cannot make a jump table to handle the switch statement. In such cases, the jump table would be huge in size and filled very sparingly. Thus the compiler resorts to using a cascade of comparisons to implement the switch. The code generated for the switch statement in such cases will look more like a series of if-else-if statements. Here the time taken to execute the switch statement increases with the number of case legs in the switch.

For other sparsely distributed big switch cases,some compilers use binary search to select the case leg. The different case values are sorted by the compiler at compile time for a binary search.

3 Likes

For smaller number of conditions, there is no difference between if-else and switch. The difference in speed comes only when there are a lot of conditions. So, when there are many conditions, switch is recommended.

2 Likes

Both use for Conditional statements but Normally we like to use Switch “True” or “False” case. I mean Boolean expression. If-Else we use rest of all conditional statements. Actually, I have posted my question related to my essay writing reviews but when i saw your question like to put answer which offcourse useful for you.

@kay_kay i also know this but can you explain me the reason why switch is much faster as compared to if-else for more number of conditions?

2 Likes

switch acts like a goto statement.Hence it goes to the desired case label without executing other labels whereas an if-else statement checks all conditions before entering into a particular block.

2 Likes