** switch Statement :-
A switch Statement is used when there is a choice as to which code to execute , depending on the value of a constant expression. Different cases are presented, and are checked one by one to see if one case matches the value of the constant Expression. If a case matches, its block of code is executed. If none of the cases match, the default code is executed.
- Syntax of the switch statement :-
switch(integer or character expression)
{
case constant_1:
statement sequence 1;
break;
case constant_2:
statement sequence 2;
break;
case constant_n:
statement sequence ;
break;
default :
statement sequence ; //end of switch statement
}
- The Flowchart of switch Statement :
 |
Operation of switch
|
- Some Programs Using switch Statement :
Q ) Make a Calculator using switch Statement :
Main program :-
#include <stdio.h>
int main()
{
float a,b,c;
char ch;
printf("enter the 1st number");
scanf("%f",&a);
printf(" enter the operator [+,-,*,/]\n");
scanf(" %c",&ch);
printf("enter the 2nd number");
scanf("%f",&b);
switch (ch)
{
case'+':c=a+b;
printf("sum of it's=%f",c);
break;
case'-':c=a-b;
printf("subtraction of it's=%f",c);
break;
case'*':c=a*b;
printf("Multiplication of it's=%f",c);
break;
case'/':c=a/b;
printf("division of it's=%f",c);
break;
default:
printf("invalid operator");
break;
}
}
Input :
IF YOU HAVE ANY QUARRIES SO YOU CAN DIRECTLY CONTACT WITH US,GO INTO THE CONTACT INFORMATION PAGE AND THEN TYPE YOUR QUARRIES .
THANKS FOR VISITING . IF YOU LOVE PROGRAMMING THEN YOU CAN FOLLOW OUR WEBSITE .....
It is really helpful
ReplyDelete