For video
tutorial you can see below video:
What is a Switch Statement?
Switch statement tests the value of
a variable and compares it with multiple cases. Once the case match is found, a
block of statements associated with that particular case is executed.
Each case in a block of a switch has a
different name/number which is referred to as an identifier. The value provided
by the user is compared with all the cases inside the switch block until the
match is found.
If a case match is NOT found, then the
default statement is executed, and the control goes out of the switch block.
Syntax for Switch Case:
switch (expression)
{
case constatnt1:
// statements
break;
case constant2:
// statements
break;
.
.
.
default:
//default statements
}
Find Example :
Example 1 :-
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter Your Choice: \n");
scanf("%d",&a);
switch(a)
{
case 1: printf("Today is Sunday");
break;
case 2: printf("Today is Monday");
break;
case 3: printf("Today is Tuesday");
break;
case 4: printf("Today is Wednesday");
break;
case 5: printf("Today is Thursday");
break;
case 6: printf("Today is Friday");
break;
case 7: printf("Today is Saturday");
break;
default: printf("Wrong Choice , Please Select No between 1 to 7");
break;
}
getch();
}
Output : -
User enter 1 then they get output “Today is monday”
Same like they enter number till 7 they get messages but if he/she enter a number is not between 1 to 7 then they get message ("Wrong Choice , Please Select No between 1 to 7");
Example 2 :-
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter The Value of A: \n");
scanf("%d",&a);
printf("Enter The Value of B: \n");
scanf("%d",&b);
printf("Enter Your Choice: \n");
scanf("%d",&c);
switch(c)
{
case 1: c=a+b;
printf("Addition of 2 digit is %d",c);
break;
case 2: c=a-b;
printf("Subtraction of 2 digit is %d",c);
break;
case 3: c=a*b;
printf("Multiplication of 2 digit is %d",c);
break;
case 4: c=a/b;
printf("Division of 2 digit is %d",c);
break;
default: printf("Wrong Choice , Please Select No between 1 to 4");
break;
}
getch();
}
Output : -
User enter 1 then they get output “Addition of 2 digit is”
Same like they enter number till 4 they get messages but if he/she enter a number is not between 1 to 4 then they get message ("Wrong Choice , Please Select No between 1 to 4");
Example 3 :-
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter The Value of A: \n");
scanf("%d",&a);
printf("Enter The Value of B: \n");
scanf("%d",&b);
printf(" * * * * * * * * * * * * * * * * * * * * \n");
printf("Press 1 for Addition \n");
printf("Press 2 for Subtraction \n");
printf("Press 3 for Multiplication \n");
printf("Press 4 for Division \n");
printf(" * * * * * * * * * * * * * * * * * * * * \n");
printf("Enter Your Choice: \n");
scanf("%d",&c);
switch(c)
{
case 1: printf("Addition of 2 digit is %d",a+b);
break;
case 2: printf("Subtraction of 2 digit is %d",a-b);
break;
case 3: printf("Multiplication of 2 digit is %d",a*b);
break;
case 4: printf("Division of 2 digit is %d",a/b);
break;
default: printf("Wrong Choice , Please Select No between 1 to 4");
break;
}
getch();
}
Output : -
User enter 1 then they get output “********************
Addition of 2 digit is
********************”
Addition of 2 digit is
********************”
Same like they enter number till 4 they get messages but if he/she enter a number is not between 1 to 4 then they get message ("Wrong Choice , Please Select No between 1 to 4");
Example 4 :-
#include<stdio.h>
#include<conio.h>
void main()
{
char a;
clrscr();
printf("Enter Your Choice: \n");
scanf("%c",&a);
switch(a)
{
case 'a': printf("Today is Sunday");
break;
case 'b': printf("Today is Monday");
break;
case 'c': printf("Today is Tuesday");
break;
case 'd': printf("Today is Wednesday");
break;
case 'e': printf("Today is Thursday");
break;
case 'f': printf("Today is Friday");
break;
case 'g': printf("Today is Saturday");
break;
default: printf("Wrong Choice , Please Select Choice between a to g");
break;
}
getch();
}
Output : -
User enter a then they get output “Today is Monday”
Same like they enter number till 4 they get messages but if he/she enter a number is not between a to g then they get message ("Wrong Choice , Please Select No between a to g");
Example 5 :-
#include<stdio.h>
#include<conio.h>
void main()
{
char d;
int a,b,c;
clrscr();
printf("Enter The Value of A: \n");
scanf("%d",&a);
printf("Enter The Value of B: \n");
scanf("%d",&b);
printf(" * * * * * * * * * * * * * * * * * * * * \n");
printf("Press a for Addition \n");
printf("Press s for Subtraction \n");
printf("Press m for Multiplication \n");
printf("Press d for Division \n");
printf(" * * * * * * * * * * * * * * * * * * * * \n");
printf("Enter Your Choice: \n");
scanf("%s",&d);
switch(d)
{
case 'a':c=a+b;
printf("Addition of 2 digit is %d",c);
break;
case 's': printf("Subtraction of 2 digit is %d",c=a-b);
break;
case 'm': printf("Multiplication of 2 digit is %d",c=a*b);
break;
case 'd': printf("Division of 2 digit is %d",c=a/b);
break;
default: printf("Wrong Choice , Please Select Choice between a to d");
break;
}
getch();
}
Output : -
User enter 1 then they get output “********************
Addition of 2 digit is
Subtraction of 2 digit is
Multiplication of 2 digit is
Division of 2 digit is
********************”
Addition of 2 digit is
Subtraction of 2 digit is
Multiplication of 2 digit is
Division of 2 digit is
********************”
Same like they enter number till 4 they get messages but if he/she enter a number is not a , s , m , d then they get message ("Wrong Choice , Please Select value a , s, ,m , d");
0 Comments
If any one want learn some specific program in Computer Languages, so They all can comment there program questions.