Difference Between If-else and Switch Case

Difference Between if-else and Switch Case in Tabular Form

If-else” and “switch” are conditional statements. The key difference is that switch despatches immediately to the case concerned, typically via an indexed jump, rather than having to evaluate all the conditions that would be required in an if-elsechain, which means that code at the end of the chain is reached more slowly than code at the beginning.

Difference Between If-else and Switch Case

Comparison Chart

IF-ELSE SWITCH
If statement is used to select among two alternatives The switch statement is used to select among multiple alternatives.
If can have values based on constraints. Switch can have values based on user choice.
If implements Linear search. Switch implements Binary search.
Float, double, char, int and other data types can
be used in if condition.
Only int and char data types can be used in switch
block.
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.




if-else

  • If supports statements only for true part
  • If…else supports statements for a true part and false
if(test-expression)
{
   True-block statement
}
else
{
   False-block statement
}
statement-x;
  • If test-expression is true, then a true-block statement is executed otherwise the false-block statement is executed.
  • Every time, either a true block or a false block will be executed.
  • In both cases, statement-x will be executed immediately after that block.

if-else example

#include<stdio.h> 
void main()
          {
           int no;
           printf("Enter the number:");
           scanf("%d", &no);
           if(no%2==0)
                 printf("the number is even );
           else
                 printf("the number is odd");
           }

Switch case

  • The switch statement is a multi-way decision
  • It tests whether an expression matches any one of the constant values or
  • The general form of switch-case is as below,
switch(expression)
{
                  case const-expr 1:    statement 1;
                                        break;
                  case const-expr 2:    statement 2;
                                        break;
                  case const-expr 3:    statement 3;
                                        break;
                  default:
                          statements
}
  • expression in switch should be an integer or character expression. Float or any other data type is not
  • Each case is labeled by one or more integer-valued
  • If a case matches the expression value then execution starts at that
  • Value of all case expressions must be
  • If none of the cases are matched then the default case is executed.
switch (grade)
{
             case 1:
                    printf("Fall (F)\n"); 
                    break;
             case 2:
                    printf("Bad (D)\n"); 
                    break;
             case 3:
                    printf("Good (C)\n"); 
                    break;
             case 4:
                    printf("Very Good (B)\n"); 
                    break;
             case 5:
                    printf("Excellent (A)\n"); 
                    break;
             default:
                    printf("You have inputted alse grade\n")
                    break;
}

Rules for switch statement

  • The switch expression must be an integral type. Float or other data types are not
  • Case labels must be constant or constant
  • Case labels must be unique.
  • Case labels must end with
  • The break statement is optional.
  • The default case statement is optional
  • Nesting of a switch statement is allowed




More Difference