C Programming language

adplus-dvertising
Statements in C : Switch statement
Previous Home Next

The switch statement allows you to select from multiple choices based on a set of fixed values for a given expression. Switch statement is used to switch the multiple choices.

switch(expression){
case value1: /* execute unit of code 1 */
break;
case value2: /* execute unit of code 2 */
break;
...
default: /* execute default action */
break;
} 

Syntax

switch (integer expression)
case 1:
do this;
case2:
do this;
//default :do this;
} 

Write a program for calculator using do while loop

#include<stdio.h>    // these are header files
#include<conio.h>    // these are header files
void main()   // main function compiler read from main
{
int a,b,C,R;   // take a variables int type
char rep='y';
do{
printf("\nCalculator\n");    
// printf is a function ,using for display a message
printf("1.addition\n");
printf("2.sutraction\n");
printf("3.multiplication\n");
printf("4.division\n");
printf("enter the value of a");
scanf("%d",&a);
printf("enter the value of b");
scanf("%d",&b);
printf("enter your choice");
scanf("%d",&C);
switch(C){
case 1:R=a+b;
printf("\nresult is%d",R);
break;
case 2:R=a-b;
printf("\nresult is%d",R);
break;
case 3:R=a*b;
printf("\nresult is%d",R);
break;
case 4:R=a/b;
printf("\nresult is%d",R);
break; // if valid then then break
default:
printf("\ncondition is invalid");
}
printf("\ndo you want to continue(y/n):");
scanf("%c",&rep);
}while(rep!='n');    //printf("do you want to continue");
getch();
}   // scanf("%c",&rep); 
Previous Home Next