gpt4 book ai didi

c - C 中完整的 "for"循环语法是什么?

转载 作者:太空狗 更新时间:2023-10-29 16:17:10 27 4
gpt4 key购买 nike

我在阅读别人的代码时看到了一些非常奇怪的 for 循环。我一直在尝试在 C 语言中搜索 for 循环的完整语法解释,但这非常困难,因为“for”一词出现在不相关的句子中,这使得搜索几乎不可能有效地通过谷歌搜索。

看完this thread,我又想到了这个问题,这让我又好奇了。

此处的 for:

for(p=0;p+=(a&1)*b,a!=1;a>>=1,b<<=1);

中间条件有一个逗号分隔两段代码,这个逗号有什么作用?我理解右侧的逗号是 a>>=1b<<=1

但是在循环退出条件下,会发生什么?它会在 p==0a==1 或两者都发生时退出吗?

如果有人能帮助我理解这一点,并可能指出完整的 for 循环语法描述的方向,那就太好了。

最佳答案

逗号不排除for循环;它是逗号运算符。

x = (a, b);

会先做 a,然后做 b,然后将 x 设置为 b 的值。

for 语法是:

for (init; condition; increment)
...

这有点(暂时忽略 continuebreak)等同于:

init;
while (condition) {
...
increment;
}

因此您的 for 循环示例(再次忽略 continuebreak)等同于

p=0;
while (p+=(a&1)*b,a!=1) {
...
a>>=1,b<<=1;
}

它的行为就好像它是(再次忽略 continuebreak):

p=0; 
while (true) {
p+=(a&1)*b;
if (a == 1) break;
...
a>>=1;
b<<=1;
}

for 循环的两个额外细节在上面的 while 循环的简化转换中没有:

  • 如果省略条件,则它始终为 true(导致无限循环,除非 breakgoto 或其他中断循环)。
  • continue 的作用就好像它是在递增之前转到标签,这与 while 循环中的 continue 不同,后者会跳过递增。

此外,关于逗号运算符的一个重要细节:它是一个序列点,如 &&|| (这就是为什么我可以将它拆分为单独的语句并且保持其含义不变)。


C99 中的变化

C99 标准引入了本说明前面未提及的几个细微差别(这对 C89/C90 非常有用)。

首先,所有循环本身都是 block 。有效地,

for (...) { ... }

本身被一对牙套包裹着

{
for (...) { ... }
}

标准说:

ISO/IEC 9899:1999 §6.8.5 Iteration statements

¶5 An iteration statement is a block whose scope is a strict subset of the scope of its enclosing block. The loop body is also a block whose scope is a strict subset of the scope of the iteration statement.

基本原理中也描述了额外的大括号。

其次,C99 中的init 部分可以是一个(单个)声明,如

for (int i = 0; i < sizeof(something); i++) { ... }

现在“包裹在循环中的 block ”出现了;它解释了为什么不能在循环外访问变量 i。您可以声明多个变量,但它们必须属于同一类型:

for (int i = 0, j = sizeof(something); i < j; i++, j--) { ... }

标准说:

ISO/IEC 9899:1999 §6.8.5.3 The for statement

The statement

for ( clause-1 ; expression-2 ; expression-3 ) statement

behaves as follows: The expression expression-2 is the controlling expression that is evaluated before each execution of the loop body. The expression expression-3 is evaluated as a void expression after each execution of the loop body. If clause-1 is a declaration, the scope of any variables it declares is the remainder of the declaration and the entire loop, including the other two expressions; it is reached in the order of execution before the first evaluation of the controlling expression. If clause-1 is an expression, it is evaluated as a void expression before the first evaluation of the controlling expression.133)

Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a nonzero constant.

133) Thus, clause-1 specifies initialization for the loop, possibly declaring one or more variables for use in the loop; the controlling expression, expression-2, specifies an evaluation made before each iteration, such that execution of the loop continues until the expression compares equal to 0; and expression-3 specifies an operation (such as incrementing) that is performed after each iteration.

关于c - C 中完整的 "for"循环语法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/276512/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com