若有以下程序 #include <stdio.h> main() { int s=0, n; for (n=0; n<4; n++) { switch(n) { defau

admin2020-07-28  13

问题 若有以下程序
    #include <stdio.h>
    main()
    {  int  s=0, n;
       for (n=0; n<4; n++)  
       {  switch(n)
         {  default:  s+=4;
            case 1:  s+=1;
            case 2:  s+=2;
            case 3:  s+=3;
         }
       }
       printf("%d\n", s);
    }
则程序的输出结果是

选项 A、6
B、18
C、10
D、24

答案D

解析 第一次for循环,n的值为0,所以从default后面的语句开始执行,s+=4,s+=1,s+=2,s+=3,s的值为10,在进入第二次for循环,n的值为1,所以执行s+=1,s+=2,s+=3,s的值为16,在进入第三次for循环,n的值为2,所以执行s+=2,s+=3,s的值为21,在进入第四次for循环,n的值为3,所以执行s+=3,s的值为24。
转载请注明原文地址:https://kaotiyun.com/show/nU3p777K
0

相关试题推荐
最新回复(0)