NIELIT O Level January-2016
Q 6 B (i) : Differentiate between if and switch case
While programming, a number conditions come and a number of decisions need to be taken by a programmer. For decision making in C programming, the statements such as if, if..else, else if, switch case etc.
Though which when number of possible condition got increase in some scenario then if-else ladder implementation become complicated. So we can achieve this using switch case in simple manner with decreasing complexity.
Else If Ladder:
else if statement can be defined as a control statement which controls the statement(s) to be executed on the basis of some conditions. For example making a program to show day of the week name through proving day.
The syntax of else if ladder can be represented as:
Features of else if ladder:
· It evaluates an expression and then, the code is selected based on the true value of evaluated expression.
· Each else if has its own expression or condition to be evaluated.
· The variable data type used in the expression of else if is either integer or character.
· The decision making of the else if is dependent on zero or non-zero basis.
The switch case statement is similar to the else-if ladder as it provides multiple branching or multi-conditional processing. But, the basic difference between switch case and else if ladder is that the switch case statement tests the value of variable or expression against a series of different cases or values, until a match is found. Then, the block of code with in the match case is executed. If there are no matches found, the optional default case is executed.
The syntax of switch case can be represented as:
Features of switch case:
· The switch case statement evaluates the value of an expression and a block of code is selected on the basis of that evaluated expression.
· Each case refers back to the original expression.
· The data type that can be used in switch expression is integer type only.
· Each case has a break statement.
· The switch case takes decision on the basis of equality.
Difference between switch case and else if ladder:
So, after going through the two control statements in brief, here are the main difference between switch case and else if ladder:
1. In else if ladder, the control goes through the every else if statement until it finds true value of the statement or it comes to the end of the else if ladder. In case of switch case, as per the value of the switch, the control jumps to the corresponding case.
2. The switch case is more compact than lot of nested else if. So, switch is considered to be more readable.
3. The use of break statement in switch is essential but there is no need of use of break in else if ladder.
4. The variable data type that can be used in expression of switch is integer only where as in else if ladder accepts integer type as well as character.
5. Another difference between switch case and else if ladder is that the switch statement is considered to be less flexible than the else if ladder, because it allows only testing of a single expression against a list of discrete values.
6. Since the compiler is capable of optimizing the switch statement, they are generally considered to be more efficient. Each case in switch statement is independent of the previous one. In case of else if ladder, the code needs to be processed in the order determined by the programmer.
7. Switch case statement work on the basis of equality operator whereas else if ladder works on the basis of true false( zero/non-zero) basis.