C Programming Tutorial
Computer Tutorial

The Switch statement



The Switch statement

The control statement that allows us to make a decision from the number of choices is called a switch, or more correctly a switch case- default, since these three keywords go together to make up the control statement.
switch is a built-in multiple-branch selection statement in C, which successively tests the value of an expression against a list of integer or character constants. When a match is found, the statements associated with that constant are executed.

The general form of the switch statement is :

switch ( expression )
{
case constant 1 :
statement sequence
break ;
case constant 2 :
statement sequence
break ;
case constant 3 :
statement sequence
break ;
.
.
default :
statement sequence
}

C Program to show the use of switch statement

\* C Program to show the use of switch statement *\
# include < stdio.h >
int   main( )
{

int   n  ;
printf(" Enter the value of n : ") ;
scanf("%d ", & n) ;
switch ( n )
{

case 1 :
printf("\n I am in case 1") ;
break ;
case 2 :
printf("\n I am in case 2") ;
break ;
case 3 :
printf("\n I am in case 3") ;
break ;
case 4 :
printf("\n I am in case 4") ;
break ;
default :
printf("\n I am in default") ;

}
return ( 0 );

}