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

admin2009-04-15  20

问题 有以下程序    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               anothor
B、another       number       number
C、another       number
D、number       number

答案4

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

最新回复(0)