考虑以下 C 程序:
#include
int main() {
float sum = 0.0, j = 1.0, i = 2.0;
while (i / j > 0.0625) {
j = j + j;
printf("%f\n", sum);
};
return 0;
}
执行上述程序时,变量 sum 将被打印的次数为 _________ 。
注意:这是数字类型问题。
(一) 5
(乙) 6
(三) 4
(四) 0答案:(一)
解释:
#include
int main() {
float sum = 0.0, j = 1.0, i = 2.0;
while (i / j > 0.0625) {
j = j + j;
printf("%f\n", sum);
};
return 0;
}
根据“while”循环,
Initially i = 2.0, j = 1.0
i / j = 2.0 / 1. 0 = 2.0 > 0.0625
j = j + j
j = 2.0
printed sum = 0.0
Second time,
i / j = 1 > 0.0625
j = 4
printed sum = 0.0
Third time,
i / j = 0.5 > 0.0625
j = 8
printed sum = 0.0
Fourth time,
i / j = 0.25 > 0.0625
j = 16
printed sum = 0.0
Fifth time,
i / j = 0.125 > 0.0625
j = 32
printed sum = 0.0
Sixth time,
i / j = 0.0625 = 0.0625
It violates the greater then condition.
This time while loop will not execute.
所以,选项(A)是正确的。
这个问题的测验