case语句只是起到语句标号的作用,不在判断,直到遇到break才会跳出Switch,default从句位于Switch最后时,可以不写break;
Switch(表达式)
{
case 常量表达式:语句序列;(break;)
default:语句序列;
}
Switch与if else的区别
1.浮点类型的变量或表达式来选择,无法使用Switch
2.根据变量在某范围内决定程序流的去向,if更方便
3.像求一个数的位数上各个数字时,数次循环Switch便可以实现
4.Switch与if else可以互化
#include <stdio.h>
int main(void)
{
char ch;
int c1 = 0;
int c2 = 0;
while ((ch = getchar()) != '#')
{
switch(ch)
{
case '.' : putchar('!');
++c1;
break;
case '!' : putchar('!');
putchar('!');
++c2;
break;
default : putchar(ch);
}
}
printf("%d\n", c1);
printf("%d\n", c2);
return 0;
}