考虑下面的C程序:
# include
int main( )
{
int i, j, k = 0;
j = 2 * 3 / 4 + 2.0 / 5 + 8 / 5;
k -= --j;
for (i = 0; i < 5; i++)
{
switch(i + k)
{
case 1:
case 2: printf("\n%d", i + k);
case 3: printf("\n%d", i + k);
default: printf("\n%d", i + k);
}
}
return 0;
}
printf语句的执行次数为__________。
(A) 8
(B) 9
(C) 10
(D) 11答案: (C)
说明:以下语句使j = 2
j = 2 * 3 / 4 + 2.0 / 5 + 8 / 5;
以下语句使k = -1。
k -= --j;
切换中需要注意的一件事是,不间断。让printf语句的计数为’count’
For i = 0, the value of i+k becomes -1, default block
is executed, count = 1.
For i = 1, the value of i+k becomes 0, default block
is executed, count = 2.
For i = 2, the value of i+k becomes 1, all blocks are
executed as there is no break, count = 5
For i = 3, the value of i+k becomes 2, three blocks
after case 1: are executed, count = 8
For i = 4, the value of i+k becomes 3, two blocks
are executed, count = 10
这个问题的测验