The control statement that allows us to make a decision from the number of choices is called a switch statement.
Syntax:
switch(integer expression)
{
case constant 1:
do this;
case constant 2:
do this;
case constant 3:
do this;
default:
do this;
}
Examples:
1.//program
#include<stdio.h>
#include<conio.h>
int main()
{
int i=2;
switch(i)
{
case 1:
printf("in class 1");
break;
case 2:
printf("in class 2");
break;
case 3:
printf("in class 3");
break;
default:
printf("in default");
}
getch();
return 0;
}
output:
in class 2
2.//without break
#include<stdio.h>
#include<conio.h>
int main()
{
int i=2;
switch(i)
{
case 1:
printf("in class 1\n");
case 2:
printf("in class 2\n");
case 3:
printf("in class 3\n");
default:
printf("in default\n");
}
getch();
return 0;
Syntax:
switch(integer expression)
{
case constant 1:
do this;
case constant 2:
do this;
case constant 3:
do this;
default:
do this;
}
Examples:
1.//program
#include<stdio.h>
#include<conio.h>
int main()
{
int i=2;
switch(i)
{
case 1:
printf("in class 1");
break;
case 2:
printf("in class 2");
break;
case 3:
printf("in class 3");
break;
default:
printf("in default");
}
getch();
return 0;
}
output:
in class 2
2.//without break
#include<stdio.h>
#include<conio.h>
int main()
{
int i=2;
switch(i)
{
case 1:
printf("in class 1\n");
case 2:
printf("in class 2\n");
case 3:
printf("in class 3\n");
default:
printf("in default\n");
}
getch();
return 0;
}
output:
in class 2
in class 3
in default
//It executes the 2 case and all the remaining cases after the 2 case as the break statement is not present.
//We can write the cases in any order, not necessarily in ascending or descending order.
3.//cases arranged in random order
#include<stdio.h>
#include<conio.h>
int main()
{
int i=2;
switch(i)
{
case 1:
printf("in class 1\n");
case 3:
printf("in class 3\n");
case 2:
printf("in class 2\n");
default:
printf("in default\n");
}
getch();
return 0;
}
#include<conio.h>
int main()
{
int i=2;
switch(i)
{
case 1:
printf("in class 1\n");
case 3:
printf("in class 3\n");
case 2:
printf("in class 2\n");
default:
printf("in default\n");
}
getch();
return 0;
}
output:
in class 2
in default
4.//Arithmetic operations -break statement is not present
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,x;
printf("enter two values\n");
scanf("%d%d",&a,&b);
printf("select operation +=1 or -=2 or /=3 or *=4 or %=5\n");
scanf("%d",&x);
switch(x)
{
case 1:
printf("sum of two numbers is %d\n",a+b);
case 2:
printf("difference of two numbers is %d\n",a-b);
case 4:
printf("product of two numbers is %d\n",a*b);
case 3:
printf("division of two numbers is %d\n",a/b);
case 5:
printf("modular division results %d\n",a%b);
default:
printf("enter any one of the operations");
}
getch();
return 0;
}
output:
enter two values
10
2
+=1 or -=2 or /=3 or *=4 or %=5
1
sum of two numbers is 12
difference of two numbers is 8
product of two numbers is 20
division of two numbers is 5
modular division results 0
enter any one of the operations
5.//To print month
#include<stdio.h>
#include<conio.h>
int main()
{
int month;
printf("enter month\n");
scanf("%d",&month);
switch(month)
{
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;
}
getch();
return 0;
}
output:
enter month
5
May
No comments:
Post a Comment