"C语言虽然没有限制if else 能够处理的分支数量,但当分支过多时,用if else 处理会不太方便,而且容易出现 if else 配对出错的情况。例如,输入一个整数,输出该整数对应的星期几的英文表示:#includeint main(){ int a; printf(""Input integer number:""); scanf(""%d"",&a); if(a==1){ printf(""Monday\n""); }else if(a==2){ printf(""Tuesday\n""); }else if(a==3){ printf(""Wednesday\n""); }else if(a==4){ printf(""Thursday\n""); }else if(a==5){ printf(""Friday\n""); }else if(a==6){ printf(""Saturday\n""); }else if(a==7){ printf(""Sunday\n""); }else{ printf(""error\n""); } return 0; }运行结果:<br>Input integer number:3↙<br>Wednesday<br>对于这种情况,实际开发中一般使用 switch 语句代替,请看下面的代码:#includeint main(){ int a; printf(""Input integer number:""); scanf(""%d"",&a); switch(a){ case 1: printf(""Monday\n""); break; case 2: printf(""Tuesday\n""); break; case 3: printf(""Wednesday\n""); break; case 4: printf(""Thursday\n""); break; case 5: printf(""Friday\n""); break; case 6: printf(""Saturday\n""); break; case 7: printf(""Sunday\n""); break; default:printf(""error\n""); break; } return 0; }运行结果:<br>Input integer number:4↙<br>Thursday<br>switch 是"
"# includeint main() { char operator; double firstNumber,secondNumber; printf(""输入操作符 (+, -, *,/): ""); scanf(""%c"", &operator); printf(""输入两个数字: ""); scanf(""%lf %lf"",&firstNumber, &secondNumber); switch(operator) { case '+': printf(""%.1lf + %.1lf = %.1lf"",firstNumber, secondNumber, firstNumber + secondNumber); break; case '-': printf(""%.1lf - %.1lf = %.1lf"",firstNumber, secondNumber, firstNumber - secondNumber); break; case '*': printf(""%.1lf * %.1lf = %.1lf"",firstNumber, secondNumber, firstNumber * secondNumber); break; case '/': printf(""%.1lf / %.1lf = %.1lf"",firstNumber, secondNumber, firstNumber / secondNumber); break; // operator doesn't match any case constant (+, -, *, /) default: printf(""Error! operator is not correct""); } return 0; }"