Difference between switch case and else if statements.


Comparison of switch and else if statement
Switch
if -else statement
1.     Syntax
switch(expression)
{
Case constant1:
……
break;
…….
default:
…..
}
2.     It is fast in operation because it directly goes to the matching case for execution.
3.     It takes or includes only integer in expression or case to match the case.
4.     Switch can test only for equality.
5.     An expression is evaluated and the code is selected on the value of the expression.
6.     It does require break statement to avoid execution the block just below the current executing block.
1.     The syntax of else if is
If (condition 1)
{block}
else if(condition 2)
{block}
…..
else
{block}
2.     It is slow in operation because the program control may not directly reach to the specific condition.
3.     It can compare for integer as well as float value.
4.     It can evaluate any type of rational or logical expression.
5.     An expression is evaluated and the code is selected based on the truth value of the expression.
6.     It does not require break statement because only one of the blocks of the code is executed.

0 comments:

Feel free to contact the admin for any suggestions and help.