Switch Statement in C



                          DECISION MAKING AND BRANCHING

The Switch Statement-


  •  The switch statement used when we have many alternatives (options) and allows us to execute one alternates then used switch statement.
  • It is a selection case statement.
  • The switch statement is a substitute of if-else-if ladder statement.
  • The syntax of the switch statement is much easier to read and write.
  • The general form of the switch statement as-



switch (expression)
      case value-1:

           statement block-1;

      case value-2:

           statement block-2;

      . . . . . .

      . . . . . .


      case value-n:

           statement block-n;







Here, switch, case, break, default are keywords.


The expression should have a result of an integer or character value.

In expression not allowed float or string type.


value-1, value-2, . . . value-n are label values (constants) and

followed by colon (:) no space follows between label value and colon.

Label values define using case keyword.


Each of case label value should be unique inside a switch statement.


The break statement is used to exit (break out) from switch. It is optional.

That is two or more case labels may belong to the same statements.

If there is no break statement found in the case, all the cases will be executed
present after the matched case.


The default label is optional. If present, it will be executed when the expression
does not find a matching case label.



Working Process:

  • The Switch is a multi-way decision making statement.
  • In switch statement we pass an expression (int or char type) (condition- variable).
  • Expression value match a list of case label values, if match is found then a block of statements associated with that case is executed.
  • If matching is not found then default block will be executed. It is optional.


When we use switch … case statements?

  • It is used when there are multiple values for a variable.
  • When one of the many alternatives is to be selected, we can use a built-in multiway decision statement known as a switch.
  • The switch statement is often used for menu selection.
  • Also known as Conditional constructs.



  1.              ANSI allowed us to maximum 257 case labels in switch statement.
  2.                Switch statement may be nested (switch within switch). It is, a switch may
  3.      be part of a case statement. ANSI C permit 15 levels of nesting.






Example Program -1:

Write a program to read day number from keyboard display day name in word using switch statement.

/* C-Program read day number from keyboard display day name in word*/


int main( )

      int n;
      printf("Enter day number :");  
      scanf("%d", &n);
         case 1:
              printf("Monday \n");  
         case 2:
              printf("Tuesday \n");
         case 3:
              printf("Wednesday \n");
         case 4:
              printf("Thursday \n");
         case 5:
              printf("Friday \n");  
         case 6:
              printf("Saturday \n");
         case 7:
              printf("Sunday \n");
              printf("invalid day number \n");
      return 0;

Output 1:

Enter day number: 4

Output 2:

Enter day number: 9
Invalid day number



Leave a Reply