08 - C Switch Case Stucture

This chapter will emphasize on case control structure of C programming. This control statement lets you to take decision from a set of choices. This is called switch. Switch control is characterized by case and default keywords. In this control structure, switch keyword is followed by an integer expression. According to the result of expression the constant which is specified with case is matched. As soon as the match is found, the statements following that case is executed. If none of them matches, then the statements following default is executed. In other words, switch provides multiple branching capability to a program. 

Format of switch-case is as follows:


Explanation: Here,

  • switch, case, break, default are keywords
  • expression_to_decide is the expression which decides value with which case value is to be matched. This can be an integer or character constant.
  • block_1, block_2, block_default, etc are the respective body corresponding to each case value.


Example: /* Program to illustrate switch-case statement */

# include <stdio.h>
# include <conio.h>

void main ()
  char myname[20];
  int code;

  printf("Enter the code\n");
  printf("Enter 1 to print your first name\n");
  printf("Enter 2 to pront your last name\n");
  scanf("%d", &code);

   case 1: printf("My first name is debo\n");
   case 2: printf("My last name is sen \n");

   default: printf("I don't want to print\n");

Snapshots of the sample program is as follows:

        Figure – C Program example switch-case statement

           Figure – C Program example switch-case statement compiled output

               Figure – Output of C Program example switch-case statement

With this we conclude this chapter. Next chapter is dedicated to sequential statements. Thank you.


Like us on Facebook