Switch Statement:
A switch case statement is a multi-way decision statement. It is a simple field version of an if-else block that evaluates only one variable.
Switch Case |
Syntax of switch case:
Switch
(variable)
{
Case 1:
Statement;
Break;
Case 2:
Statement;
Break;
‘
‘
‘
‘
Case n:
Statement;
Break;
Default :
Statement;
Break;
}
Example of a Switch Case Program:
Q. Write a
menu driven program in C, press 1 for addition and press 2 for subtraction
between 2 number.
#include
<stdio.h>
#include
<conio.h>
main()
{
int a, b, c,
ch;
printf(“Press 1 for Addition.”) ;
printf(“Press 2 for Subtraction.”) ;
printf(“Enter
Your Choice”) ;
scanf(“%d”,
&ch);
switch(ch)
{
case1:
printf(“Enter
2 Number:”);
scanf(“%d%d”,
&a, &b);
c=a+b;
printf(“Addition
Result %d”, c);
break;
case2:
printf(“Enter
2 Number:”);
scanf(“%d%d”,
&a, &b);
c=a-b;
printf(“Subtraction
Result %d”, c);
break;
default:
printf(“Wrong
Choice”);
break;
}
getch();
}
0 Comments