我正在使用 CodeChef.com 上的 C 编译器。我编写了以下代码。
#include <stdio.h>
int main()
{
int scores[] = {23, 24, 25, 26, 27, 28};
int scoreslength = 0;
int i = 0;
do
{
printf("%d \n", scores[i]);
i = i + 1;
}
while (i < scoreslength);
return 0;
}
输出为23
。我明白了,因为我给出了 scoreslength
的值成为0
和 do while
循环至少执行一次,因为条件 while (i < scoreslength)
在循环末尾给出。但是当我给出 scoreslength
的值时成为1
,输出仍然是 23
。
我的问题是,当scoreslength
的值给出为1
输出不应该是 23
和24
因为循环首先在 i = 0
处执行递增后在 i = 1
处再次执行?
最佳答案
当scoreLength给定为1时,循环第一次执行时i = 0。在循环内,您递增i = i + 1,这使得i = 1。因此请检查while循环中的条件。它说i < scoreLength
其评估结果为 false。所以你的循环只执行一次。
关于c - 关于 C 语言中 do while 循环程序的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42465715/