逗号分隔的表达式在for循环中作为条件是如何工作的?

huangapple go评论73阅读模式
英文:

How do Comma-separated expressions as condition in a for-loop work?

问题

在Stack Overflow上,我已经多次在答案和问题中看到了使用逗号分隔的条件作为for循环的循环条件,例如: for(int i = 0, int j = 0 ; i < 2, j < 4; i++, j++) { ... }。这是如何工作的?

在C中,for循环中如何使用逗号分隔的条件?
例如:
for(int i = 0, int j = 1 ; i < 5, j < 4; i++, j++) { ... }

这不是无效的吗,因为条件需要是一个求值表达式吗?如何将多个逗号分隔的表达式评估为for循环中的一个表达式?

提到的表达式被分号包围,如; i < 5, j < 4;,这将是循环的条件。我错了吗?

英文:

I´ve seen a a few times already in answers and questions here on Stack Overflow, a comma-separated list as loop condition for a for-loop like : for(int i = 0, int j = 0 ; i < 2, j < 4; i++, j++) { ... }
. How does this work?

How does a comma-separated condition work in a for-loop in C?
Like:
for(int i = 0, int j = 1 ; i < 5, j < 4; i++, j++) { ... }

Isn´t this invalid because the condition needs to be an evaluated expression? How do the multiple comma-separated expressions get evaluated to one expression in the for-loop?

The mentioned expressions are enclosed by semicolons ; i < 5, j < 4;, and this shall be the condition of the loop. Or am I wrong?

答案1

得分: 8

使用带有声明的for循环的语法是(6.8.5):

for (声明 表达式_可选 ; 表达式_可选) 语句

这个:

for(int i = 0, int j = 1 ; i < 5, j < 4; i++, j++) { }

无法解析,因为

int i = 0, int j = 1 ; 在语法上不是一个有效的声明(或者说在C中的任何句子形式都不是),但是

for(int i = 0, j = 1; i < 5, j < 4; i++, j++) { }

可以解析,因为

int i = 0, j = 1;

是一个有效的声明(带有多个包含初始值的声明符),而且

i < 5, j < 4

是一个有效的逗号操作符表达式,就像

i++, j++

一样。

(注意,i < 5, j < 4 的效果与只有 j < 4 一样,因为逗号表达式从左到右计算,并且最后一部分的值被使用,而且因为 i < 5 在这里没有副作用,所以整个表达式等同于只有 j < 4。也许你想要使用 i < 5 && j < 4 代替。)

在不带声明的for循环的原始形式中,你有(6.8.5):

for (表达式_可选 ; 表达式_可选 ; 表达式_可选) 语句

分号用于分隔这3个(可选的)表达式。

在包含声明的形式中,第一个分号被解释为声明的一部分:

for (声明 表达式_可选 ; 表达式_可选) 语句
英文:

The syntax for a for loop with a declaration is (6.8.5):

for ( declaration expression_opt ; expression_opt ) statement

This

for(int i = 0, int j = 1 ; i < 5, j < 4; i++, j++) { }

doesn't parse because

int i = 0, int j = 1 ; isn't syntactically valid as a declaration (or any sentential form in C for that matter) but

for(int i = 0, j = 1; i < 5, j < 4; i++, j++) { }

would parse, because

int i = 0, j = 1;

is a valid declaration (with multiple, initializer-containing declarators) and

i < 5, j < 4

is a valid comma-operator-containing expression, as is

i++, j++

.

(Note that i < 5, j < 4 has the same effect as just j < 4 because comma expressions are evaluated left to right and the value of the final part is used, and since i < 5 has no side-effect here, the whole expression is equivalent to just j < 4. You might have wanted i < 5 && j < 4 instead.)

In the original form of the for loop (without a declaration) you have (6.8.5)

 for ( expression_opt ; expression_opt ; expression_opt ) statement

and the semicolons are used to separate the 3 (optional) expressions.

In the declaration-containing form, the first semicolon is parsed as part of the declaration

for ( declaration expression_opt ; expression_opt ) statement

.

答案2

得分: 1

逗号分隔列表中的所有表达式都会被评估,但只有最后一个表达式用于for循环的条件。因此,实际上会忽略i<5的条件,因为它不是最后一个表达式。

英文:

All the expressions in the comma-separated list are evaluated, but only the last expression is used for the for loop condition. So in effect the i<5 condition is ignored, since it's not the last expression.

huangapple
  • 本文由 发表于 2020年1月6日 19:22:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定