Switch-Case Statement in C
Switch-Case statement in C allows us to
execute multiple operations for the different possible values of a single
variable called switch variable. Here, We can define various statements in the
multiple cases for the different values of a single variable. A switch statement
allows a variable to be tested for equality against a list of values. Each
value is called a case, and the variable being switched on is checked for
each switch case.
Syntax :
switch
(expression)
{
case
value1:
{
Statements
of case 1
break;
}
case
value2:
{
Statements
of case 2
break;
}
case
value3:
{
Statements
of case 3
break;
}
.....
default:
{
code to be executed if all cases are not
matched;
}
} //End of switch case statement
Rules for switch statement in C language
Ø The
case value must be an integer or character constant.
Ø The
case value can be used only inside the switch statement.
Ø The
switch expression must be of an integer or character type.
Flowchart of switch statement in C
Example Program :
//Example program of swith case statement
//satyamsrig.blogspot.in
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int ch;
printf("Enter the number of month\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
printf("January");
break;
}
case 2:
{
printf("February");
break;
}
case 3:
{
printf("March");
break;
}
case 4:
{
printf("April");
break;
}
case 5:
{
printf("May");
break;
}
case 6:
{
printf("June");
break;
}
case 7:
{
printf("July");
break;
}
case 8:
{
printf("August");
break;
}
case 9:
{
printf("September");
break;
}
case 10:
{
printf("October");
break;
}
case 11:
{
printf("November");
break;
}
case 12:
{
printf("December");
break;
}
default :
{
printf("You have entered wrong keyword");
}
}
getch();
}
Example Program Youtube Video :
No comments:
Post a Comment