If…else…if vs switch…case, difference and usage?

if...else...if and switch...case both programming constructs has ability to take decision based on conditions. Both are almost similar in nature. However, there is always a debate among beginners which to use and when to use what?

In this post, I will compare both on various grounds. So let us begin.

Before you being have a quick look on –

Working mechanism

if...else...if checks all conditions sequentially until condition matched. It skips all subsequent condition check once condition got matched.

On the other hand, working mechanism of switch...case is completely different. During the compilation process, C compiler generate a lookup table based on the case values. On execution, instead of matching switch(expression) for each case, it query the lookup table generated during compilation. If the case exists in lookup table, then it transfers control to the matching case otherwise to default case (if mentioned).

Performance

Talking about the performance, switch...case wins the race. During the compilation process, switch...case generate a lookup table. Using lookup table, it directly transfers program control to the matching case or default case. Hence, condition checking overhead during switch...case execution is relaxed. Whereas, if statement checks all condition sequentially. Which slightly degrades the performance of if statements compared to switch.

However, its not true for every program. Compared to switch, if...else...if statement generate a better performance and code readability with fewer conditions. if...else...if statement with 1-5 conditions will generate a better performance, than switch...case with 1-5 cases. This is because, checking fewer conditions is worthy than querying a separate lookup table.

Let me explain this with an example – Let us suppose a telephone directory with 5 names printed on 5 separate pages. What is more efficient way to search name in the directory?

  1. Searching directly in each page of directory one after another.
  2. Create an index page, search the name in index page, if name exists in index page then go to that particular page.

I guarantee for this case you will select first approach.

So, I can conclude that if...else...if statements generate better performance with fewer conditions. Whereas switch...case is worthy to use for more number of fixed choices.

Complexity

Depending on situation if...else...if as well as switch...case can be simple or complex. Complexity of if...else...if statement increases with increase in conditions. At one stage if statements become confusing with increase in level of ladder if conditions. Nesting of if…else…if also increases the level of complexity.

Compared to if...else...if statements switch...case is easy to read, code and maintain. However, switch can get confusing if nested.

Limitations of switch...case

In real life switch...case has some limitation which cannot be unseen. Let us give a quick look over limitations of switch.

Remember programs written using switch...case can be transformed to if...else...if. But not all if...else...if programs can be converted to switch...case.

Final conclusion

Use if...else...if statement when –

  • There are conditions instead of list of choices.
  • There are few number of conditions.

Use switch...case when –

  • There is a list of choices, from which you need to take decision.
  • Choices are in the form of integer, character or enumeration constant.