C语言 for 循环缺少初始化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3792183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:31:26  来源:igfitidea点击:

for loop missing initialization

c

提问by Matt

I've seen

我见过

for(;;)

and

for ( ; *s != '
for ( ; *s != '##代码##'; s++)
'; s++)

Why is it blank like that. Thanks.

怎么就这样一片空白。谢谢。

回答by NullUserException

The forstatement works like:

for语句的工作方式如下:

for (initialization; test-condition; update)

for (initialization; test-condition; update)

And any or all of those three can be omitted (left blank). So:

并且这三个中的任何一个或全部都可以省略(留空)。所以:

  • for (;;)is an infinite loop1equivalent to while (true)because there is no test condition. In fact, for (int i=0; ;i++)would also be an infinite loop1.

  • for ( ; *s != '\0'; s++)is a loop with no initialization. swill point to the beginning of (probably) a string and is incremented until it reaches the null character '\0'denoting end-of-string. This essentially means loop through all characters of the string s

  • for (;;)是一个无限循环1等价于while (true)因为没有测试条件。事实上,for (int i=0; ;i++)也将是一个无限循环1

  • for ( ; *s != '\0'; s++)是一个没有初始化的循环。s将指向(可能)一个字符串的开头并递增,直到它到达'\0'表示字符串结尾的空字符。这实质上意味着遍历字符串的所有字符s

1The loop will still be interrupted if there's a breakstatement in the loop body, or a call to exit(), etc...

1如果break循环体中有语句或调用exit()等,循环仍将被中断...

回答by AnT

It is "blank like that" because the author of the code left it blank. The author did not want/need to do anything in the corresponding section of forstatement, so it was left blank.

它是“像那样的空白”,因为代码的作者将其留空。作者不想/不需要在for声明的相应部分做任何事情,因此留空。

for (;;)is a statement that iterates indefinitely (unless it is interrupted from inside cycle body).

for (;;)是一个无限迭代的语句(除非它被循环体内部中断)。

for ( ; *s != '\0'; s++)is a statement that does not need an initialization section, since everything necessary (like the initial value of s) was already initialized before that forstatement.

for ( ; *s != '\0'; s++)是一个不需要初始化部分的语句,因为所有必需的(如 的初始值s)在该for语句之前已经初始化。

回答by Justin Ardini

for(;;)is an infinite loop. It is effectively the exact same as while (true).

for(;;)是一个无限循环。它实际上与while (true).

The reason this works is because when the middle condition in a forloop is empty, it is interpreted as always being true.

这样做的原因是因为当for循环中的中间条件为空时,它被解释为始终为真。

for ( ; *s != '\0'; s++)is used for reading strings character-by-character. This approach works because every C string ends with a null character (\0).

for ( ; *s != '\0'; s++)用于逐个字符读取字符串。这种方法有效,因为每个 C 字符串都以空字符 ( \0)结尾。

回答by casablanca

The parts that are blank essentially do nothing. So for (;;)creates an infinite loop that does nothing at all, and never exits because there is no condition in the loop. Your second example:

空白的部分基本上什么都不做。所以for (;;)创建了一个什么都不做的无限循环,并且永远不会退出,因为循环中没有条件。你的第二个例子:

##代码##

is just a normal loop without any initialization expression. This relies on the fact that salready has an initial value and just loops until it reaches the end of the string.

只是一个没有任何初始化表达式的普通循环。这依赖于这样一个事实,即s已经有一个初始值并且只是循环直到它到达字符串的末尾。

回答by Mawg says reinstate Monica

it means do forever

这意味着永远做

for (initial condition; increment; end condition); You can omit any of these

for(初始条件;增量;结束条件);你可以省略任何这些

See http://en.wikipedia.org/wiki/For_loop

http://en.wikipedia.org/wiki/For_loop

The three control expressions, separated by semicolons here, are from left to right the initializer expression, the loop test expression, and the counting expression. The initializer is evaluated exactly once right at the start of the loop. The loop test expression is evaluated at the beginning of each iteration through the loop, and determines when the loop should exit. Finally, the counting expression is evaluated at the end of each loop iteration - even if continue is called - and is usually responsible for altering the loop variable.

In most languages which provide this type of for loop, each of the three control loop expressions is optional. When omitted the loop test expression is taken to always be true, while the initializer and counting expressions are treated as no-ops when omitted. The semicolons in the syntax are sufficient to indicate the omission of one of the expressions.

三个控制表达式,这里用分号分隔,从左到右依次是初始化表达式、循环测试表达式和计数表达式。初始化器在循环开始时只计算一次。循环测试表达式在循环的每次迭代开始时进行评估,并确定循环应何时退出。最后,计数表达式在每次循环迭代结束时进行计算——即使调用 continue——并且通常负责改变循环变量。

在大多数提供这种 for 循环的语言中,三个控制循环表达式中的每一个都是可选的。省略时,循环测试表达式被视为始终为真,而初始化器和计数表达式在省略时被视为无操作。语法中的分号足以表示省略了其中一个表达式。

回答by Vlasin

while(1)and while(true)are the same as for(;;)

while(1)并且while(true)for(;;)