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 caseetc. have defined in the standard C library. In case of multi-conditional processing, basically there come two statements in the choice of a programmer; they are switch case and else if ladder. In this post, I have presented the features and applications of the two statements, and the difference between switch case and else if ladder.
Though the function of switch case and else if ladderis same, there are a number of remarkabledifference between switch case and else if ladder in many aspects such as memory consumption, speed of processing, variable requirement, comfort in programming etc. Appropriate choice between switch case and if else ladder is essential for the sake of ease, comfort, accuracy and efficient programming.
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. Whenever the else if statement is used, the compiler or interpreter initially checks the condition whether it is true or false and if the condition is found to be true then, the corresponding statements are executed. If the condition is found to be false, it continues checking the next else if statement until the condition comes to be true or the control comes to the end of the else if ladder.
The syntax of else if ladder can be represented as:
Below is a flowchart that represents else if ladder.
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.
Switch Case:
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:
Below is a flowchart that represents switch case.
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:
- 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.
- The switch case is more compact than lot of nested else if. So, switch is considered to be more readable.
- The use of break statement in switch is essential but there is no need of use of break in else if ladder.
- 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.
- 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.
- 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.
- 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.
No comments:
Post a Comment