What is the difference between switch and if else statements? Which is better in different cases? Why?

This is the general syntax of if-else ladder:
  1. if (condition1) { //Body of if }
  2. else if (condition2) { //Body of if }
  3. else if (condition3) { //Body of if }

 
And this is the general syntax for switch:
  1. switch ( variable )
  2. {
  3. case <variable value1>: //Do Something
  4. break;
  5. case <variable value2>://Do Something
  6. break;
  7. default: //Do Something
  8. break;
  9. }

 
The if-else ladder is of type strict condition check,
while switch is of type jump value catching.
 
Limitations of switch over if-else ladder:
  • The variable expression are also not allowed in cases.
    case i+2:
     is not allowed in switch, but is vaild on if-else.
  • You cannot test a flat expression using switch.
  • You cannot use similar expressions for multiple cases. For instance below switch statement in c is illegal:
    1. switch (expression)
    2. {
    3. case 5: //...
    4. break;
    5. case 2+3: //...
    6. break;
    7. }
  • switch statement must know the value inside it during compilation. But in c programming most of the time, it is require depending on the user input. In such cases, you cannot use switch.

Advantages of switch over if-else ladder:
  • A switch statement works much faster than equivalent if-else ladder.
    It is because compiler generates a jump table for a switch during compilation. Consequently, during execution, instead of checking which case is satisfied, it only decides which case has to be executed.
  • It is more readable and in compare to if-else statements.
  • It is more manageable for having higher level of indentation than if. For instance check below two source codes (solving same problem one using if where the other using switch) to check error messages.
Where to use switch over if-else ladder:
  • If there are large number of compares for a condition in your program, use switch over if-else ladder.
  • For more complex comparisons.

Where to use if-else ladder over switch:
In case of simple and few compares, if-else executes faster and easy write. Thus as per program’s requirement, a programmer should decide himself where to use which one condition control.




Alternate Answer


Basically the switch statements and nested if else statements are used when there are multiple choices and only is to be executed. 
Difference Between Nested 'If-Else' and 'Switch'
Switch statements is very easy to use when there are multiple choices. 

If-Else statements is very difficult to used when there are many choices.

Switch Statements, it cannot check the range of values.

In If-Else statements, it can be check the range of values.

In Switch Statements, it can use only single expression for multiple choices.

In If-Else , it can use multiple expressions for multiple choices.

Comments

Popular posts from this blog

Java program to display triangle 1 23 456 78910

Google Invests Heavily In Canada For AI Lab