20180601 C++

下列程序的输出结果是()。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
main()
int x=1,y=0,a=0,b=0;
switch (x)
{
case 1: switch (y)
{
case
0:a++; break ;
case
1:b++; break ;
}
case 2:a++;b++; break ;
case 3:a++;b++; break ;
}
printf ( "a=%d,b=%d\n" ,a,b);
}
a=1,b=0
a=2,b=1
a=1,b=1
a=2,b=2

答案:B 错选:A
因为switch x=1 执行x=1的情况case 到了switch y=0 执行y=0的情况case a加一次 因为break语句 针对第二个switch 不执行以后的case 针对第一个switch case1没有break 继续执行第二个case2 a加一次 b加一次 因为break语句 针对第一个switch 不执行以后的case 总结 a加两次 b加一次 开始 a是 0 b是 0 输出 a是 2 b是 1
case 1分支没有break,所以接着运行case 2后面的分支

猜你喜欢

转载自www.cnblogs.com/kxzh/p/9150393.html
C++