while 循环检查以逗号分隔的两个值

while loop check for two values seperated by comma

我遇到过一些看起来像这样的代码。

while(1, N) 

其中 N 可以是 0 - 100 之间的整数。

有人能告诉我们如何使用这样的 while 循环吗?

while(1, N)

等同于

while(N)

因为逗号运算符产生右操作数的值。所以使用第一种形式是没有用的。

如果你想写一个运行从1N(包括)的循环,你可以使用for循环:

for (int i = 1; i <= N; i++)

根据 C11 标准文档,第 6.5.17 章,逗号运算符,

The left operand of a comma operator is evaluated as a void expression; there is a sequence point between its evaluation and that of the right operand. Then the right operand is evaluated; the result has its type and value.

所以,本质上,

while(1, N)

相同
 while(N)