switch statement in c

 switch statement in c

switch statement used to select any one case among available cases, switch statement only test for matching value in expression when we declare Case values it should be unique and  it Can be defines using  keyword case and its  value should not be decimal or floating point.

Every case  in switch statement must be terminate with break except default , by using break is come out of the switch statement.

syntax of switch statement is as follow.

switch(integer expression)

{

  case constant1:

  Statement1;

  break;

 case constant2:

  Statement2;

  break;

. . . . .

 default:

 Statement;

}

 

Flow chart of Switch statement:

Switch statement in c


Example of switch Statement

#include<stdio.h>

#include<conio.h>

Void main()

{

int choice;

clrscr();

printf("\nEnter number of day:"); scanf("%d",&choice); switch(choice)

{

case 1: printf("\n The day is Monday ");

break;

case 2: printf("\n The day is Tuesday”);

break;

case 3:printf("\n The day is Wednesday”);

break;

case 4:printf("\n The day is Thursday");

break;

case 5:printf("\n The day is Friday");

break;

case 6:printf("\n The day is Saturday");

break;

case 7:printf("\n The day is Sunday");

break;

default: printf("\n Wrong day entered!");

}

getch();

}

 

Previous
Next Post »