多重选择:switch 和break
       //7-11.c使用switch
#include <stdio.h>
#include <ctype.h>
int main (void)
{
       char ch;
       printf ("Give me a letter of the alphabet ,and I will give ");
       printf ("an animal name \nbeginning with that letter.\n");
       printf ("Pleaxe type in a letter:type # to end my act.\n");
       while ((ch = getchar ()) != '#')
       {
      
              if ('\n' == ch)
                     continue;
              if (islower (ch))//只识别小写字母
                     switch (ch)
              {
                     case 'a':
                            printf ("argali ,a wild sheep of Asig\n");
                            break ;
                     case 'b':
                            printf ("babirusa ,a wild pig of Malay\n");
                            break;
                     case 'c':
                            printf("coati,racoonlike mammal\n");
                            break;
                     case 'd':
                            printf ("desman ,aquatic,molelike critter\n");
                            break;
                     case 'e':
                            printf ("echidna ,the spiny anteater\n");
                            break;
                     case 'f':
                            printf ("fisher ,brownish maren\n");
                            break;
                     default :
                            printf ("That's a stumper!\n");
 
              }
              else
                     printf("I recognize only lowercase letters.\n");
              while (getchar () != '\n')
                     continue;
              printf("Pleass type another letter or a # .\n");
       }
       printf ("Bye!\n");
       return 0 ;
}
紧跟在单词switch后的圆括号里的表达式被求值。在这个程序中,它就是刚刚输入给ch的值,然后程序扫描标签列表(case ‘a’)直到搜索到一个与该匹配的标签,然后程序跳到那一行,要没有匹配的标签,如果有被标记default的标签行,程序就跳到该行;否则,程序继续处理跟在switch语句之后的语句。而break语句,它可以使用程序脱离switch语句,如果没有它,程序会从相匹配的标签到switch末尾的每一条语句都将被处理,break 可以用于switch语句而continue 只能用在循环中,还有就是case标签必须是整型常量或者整数常量表达式,不能用变量作为case标签。

 

 

 

只读取一行中的首个字符:
       while (getchar () != '\n')
              continue;
这个循环从输入读取字符,直到出现由回车键产生的换行字符。注意,函数返回值没有被赋给ch,因此,字符仅被读取并丢弃。因为最后一个被丢弃的字符是换行符,所以下一个读入的字符是下一行的首字符。在外层while循环中由getchar()读取它并将其值赋给ch.
多重标签:
       //7-12.c
#include <stdio.h>
int main (void)
{
       char ch;
       int a_ct,e_ct,i_ct,o_ct,u_ct;
       a_ct = e_ct = i_ct = o_ct = u_ct = 0;
       printf ("Enter some text :enter # to quit.\n");
       while ((ch = getchar ()) != '#')
       {
              switch (ch)
              {
              case 'a':
              case 'A':a_ct ++;
                            break;
              case 'e':
              case 'E':e_ct ++;
                            break ;
              case 'i':
              case 'I':i_ct ++;
                            break;
              case 'o':
              case 'O':o_ct ++;
                            break;
              case 'u':
              case 'U':u_ct ++;
                            break;
              default :break;
              }
       }
       printf ("number of vowels:A   E   I   O   U\n");
       printf ("               %4d%4d%4d%4d%4d\n",a_ct,e_ct,i_ct,o_ct,u_ct);
       return 0;
}
 
在此程序中可此不要bresk语句,程序还是会进行switch结构的下一个语句即:default :break语句。
什么时候用switch和if eles :在这里可以是没有选择的因为switch的case标签只能是整型常量或整数常量表达式,所以其它的只能选择if eles语句。如果是很小的整数范围则可以用switch语句。