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?
if-else and switch both are selection statements. The selection statements, transfer the flow of the program to the particular block of statements based upon whether the condition is true or false. The fundamental difference between if-else and switch statements is that the if-else statement “selects the execution of the statements based upon the evaluation of the expression in if statements”. The switch statements “selects the execution of the statement often based on a keyboard command”.
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?
So, we can say 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.
Comparison Chart
Basis for Comparison | if-else | switch |
---|---|---|
Basic | Which statement will be executed depend upon the output of the expression inside if statement. | Which statement will be executed is decided by user. |
Expression | if-else statement uses multiple statement for multiple choices. | switch statement uses single expression for multiple choices. |
Testing | if-else statement test for equality as well as for logical expression. | switch statement test only for equality. |
Evaluation | if statement evaluates integer, character, pointer or floating-point type or boolean type. | switch statement evaluates only character or integer value. |
Sequence of Execution | Either if statement will be executed or else statement is executed. | switch statement execute one case after another till a break statement is appeared or the end of switch statement is reached. |
Default Execution | If the condition inside if statements is false, then by default the else statement is executed if created. | If the condition inside switch statements does not match with any of cases, for that instance the default statements is executed if created. |
Editing | It is difficult to edit the if-else statement, if the nested if-else statement is used. | It is easy to edit switch cases as, they are recognized easily. |
Final conclusion
Use if...else...if statement when -
Use switch...case when -