c - 关于 C 语言中 do while 循环程序的输出

标签 c

我正在使用 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 的值成为0do while循环至少执行一次,因为条件 while (i < scoreslength)在循环末尾给出。但是当我给出 scoreslength 的值时成为1 ,输出仍然是 23

我的问题是,当scoreslength的值给出为1输出不应该是 2324因为循环首先在 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/

相关文章:

c - 在不使用运算符的情况下添加两个数字

c - 为什么 "unused attribute"为结构数组生成警告?

c - 如何检测是否可以用C打开浏览器

c - Linux内核为模块生成compile-commands.json

c - 模块化C代码有哪些方法?

c - 向指针添加变量不同于向常量添加

c++ - 有没有办法减少 GNU make 中重复的先决条件模式?

C:使用 memcpy 分配结构

c - 帮助自定义 getline() 函数

C 错误 TLS 错误