C语言编程,while循环

标签 c

我正在阅读代码,发现了一个 while 循环,其结构如下:

while(q--)

我知道这是一个后减运算符,但我不明白 while 循环中正在测试的条件

感谢您抽出时间,康纳

最佳答案

while 循环检查条件是否为真。

计算结果为 0 的表达式被视为 false,而任何非零表达式都被视为 true。

摘自 C Standard 的第 6.8.5 节:

4 An iteration statement causes a statement called the loop body to be executed repeatedly until the controlling expression compares equal to 0. The repetition occurs regardless of whether the loop body is entered from the iteration statement or by a jump

因此这个循环测试 q 是否非零。一旦q减至0,循环就会退出。

关于C语言编程,while循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40476461/

相关文章:

c - 收到警告 "implicit declaration of function ' strlen'"

c - MinGW 的 msvcrt 替代品? (例如,获得符合要求的 snprintf)

python - 如何从 PyArrayObject 获取值?

c - 当计算结果为 0 的整数枚举用作指针时,gcc 是否应该发出警告?

c++ - 导致编译器发出虚假警告的场景有哪些?

c - 如何使用 OpenMP 加速 C++ 代码?

c - 使用 MPI_THREAD_MULTIPLE 的混合循环并行化

C 从文本中读取多个结构

c - 仅访问结构的某些成员而不是所有成员

c - 宏连接,函数名生成