有以下程序: main () { char k; int i; for(i=1;i<3;i++) { scanf("%c",&k); switch(k) {

admin2013-02-23  27

问题 有以下程序:    main ()    {      char k;    int  i;      for(i=1;i<3;i++)      {        scanf("%c",&k);          switch(k)         {         case  ’0’:  printf("another\n");         case  ’1’:  printf("number\n");         }        }       }         程序运行时,从键盘输入:01<回车>,程序执行后的输出结果是

选项 A、another   number
B、another   number   another
C、another   number   number
D、number   number

答案4

解析 switch语句的执行过程是:在switch后面的表达式的值和case后面常量表达式的值吻合时,就执行后面的语句.如果在该语句的后面没有break语句,则继续执行下一个case,直到遇到break语句或switch多分支的结束,在 switch语句中,break语句的作用是使流程跳出switch结构,终止Switch语句的执行.本题中在for循环中嵌套了swish语句,每循环一次通过scanf()函数从键盘上输入一个k值,然后执行switch语句。for循环共循环了2次,当i=1时,从键盘上输入0,使得k的值为0,执行switch语句中case:0后面的语句,输出another,接着执行case:0下面的语句输出number,退出switch语句,当i=2时,从键盘上输入1,使得k的值为1,执行switch语句中case 1后面的语句,输出number,退出switch语句.当 i=3时退出循环.故最后的输出为another、number和number,所以,4个选项中选项C符合题意。
转载请注明原文地址:https://kaotiyun.com/show/ApPp777K
0

最新回复(0)