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 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
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 supports statements only for true part
  • If…else supports statements for a true part and false
   True-block statement
   False-block statement
  • 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

void main()
           int no;
           printf("Enter the number:");
           scanf("%d", &no);
                 printf("the number is even );
                 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,
                  case const-expr 1:    statement 1;
                  case const-expr 2:    statement 2;
                  case const-expr 3:    statement 3;
  • 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"); 
             case 2:
                    printf("Bad (D)\n"); 
             case 3:
                    printf("Good (C)\n"); 
             case 4:
                    printf("Very Good (B)\n"); 
             case 5:
                    printf("Excellent (A)\n"); 
                    printf("You have inputted alse grade\n")

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