switch 是一个开关语句,和case配套使用, 和if else 判断语句差不多, switch 语句是用于多分支语句进行条件判断。下例为用switch语句编写的四则运算:
#include
void main()
{
double N1,N2;
char Operation;
printf("输入运算的两个数");
scanf("%lf%lf",&N1,N2);
printf("请输入运算符(+,-,*,/):");
switch(Operation)
{
case '+':printf("%lf%c%lf=%lf",N1,N2,Operation,N1+N2);
break;
case '-':printf("%lf%c%lf=%lf",N1,N2,Operation,N1-N2);
break;
case '*':printf("%lf%c%lf=%lf",N1,N2,Operation,N1*N2);
break;
case '/':printf("%lf%c%lf=%lf",N1,N2,Operation,N1/N2);
break;
}
}