C++ “for(;;)”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4894120/
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-08-28 16:52:15  来源:igfitidea点击:

What does "for(;;)" mean?

c++csyntaxfor-loop

提问by sas4740

In C/C++, what does the following mean?

在 C/C++ 中,以下是什么意思?

for(;;){
    ...
}

回答by Justin Ardini

It's an infinite loop, equivalent to while(true). When no termination condition is provided, the condition defaults to true.

这是一个无限循环,相当于while(true). 当未提供终止条件时,条件默认为true

回答by paxdiablo

In C and C++ (and quite a few other languages as well), the forloop has three sections:

在 C 和 C++(以及许多其他语言)中,for循环包含三个部分:

  • a pre-loop section, which executes before the loop starts;
  • an iteration condition section which, while true, will execute the body of the loop; and
  • a post-iteration section which is executed after each iteration of the loop body.
  • 预循环部分,在循环开始之前执行;
  • 一个迭代条件部分,当它为真时,将执行循环体;和
  • 在循环体每次迭代后执行的迭代后部分。

For example:

例如:

for (i = 1, accum = 0; i <= 10; i++)
    accum += i;

will add up the numbers from 1 to 10 inclusive.

将把 1 到 10 的数字相加。

It's roughly equivalent to the following:

它大致相当于以下内容:

i = 1;
accum = 0;
while (i <= 10) {
    accum += i;
    i++;
}

However, nothing requires that the sections in a forstatement actually containanything and, if the iteration condition is missing, it's assumed to be true.

但是,没有任何要求for语句中的部分实际上包含任何内容,如果缺少迭代条件,则假定它为真。

So the for(;;)loop basically just means:

所以for(;;)循环基本上只是意味着:

  • don't do any loop setup;
  • loop forever (breaks, returns and so forth notwithstanding); and
  • don't do any post-iteration processing.
  • 不要做任何循环设置;
  • 永远循环(尽管有中断、返回等);和
  • 不做任何后期迭代处理。

In other words, it's an infinite loop.

换句话说,这是一个无限循环。

回答by Tony Delroy

Loop until some break, exit, throwetc. statement inside the loop executes. Basically, you can think of a forloop as consisting of:

循环直到循环内的some break, exit, throwetc. 语句执行。基本上,您可以将for循环视为由以下部分组成:

for (setup; test; advance)
    ...

If the "test" is empty it's considered to be true, and the loop keeps running. Empty "setup" and "advance" simply do nothing.

如果“测试”为空,则认为是true,并且循环继续运行。空的“设置”和“提前”只是什么都不做。

回答by bhasinusc

An infinite loop which continues until there is a break, exit, or gotostatement.

一个无限循环,一直持续到出现break, exit, orgoto语句。

回答by Jean-Fran?ois Fabre

Even if this answersuggests that both constructs are equivalent, there's a subtle difference between both for(;;)and while(1)(which both create infinite loops) in the C language (and possibly compiler-dependent).

即使这个答案表明这两个构造是等效的,但在 C 语言(并且可能依赖于编译器)中for(;;)while(1)(两者都创建无限循环)之间存在细微的差异。

Some compilers (Windriver DIABData for instance) complain about "condition is always true" when using while(1).

一些编译器(例如 Windriver DIABData)在使用while(1).

Changing to for(;;)allows to get rid of the warning, probably because the latter expression is semantically stronger to create an infinite loop on purpose, and there is no "always true" condition at all (plus it's shorter to write).

更改为for(;;)允许摆脱警告,可能是因为后一个表达式在语义上更强大,可以故意创建无限循环,并且根本没有“始终为真”的条件(而且编写时间更短)。

On the other hand, the C++ language doesn't make a difference about both constructs as Adrian stated in comments:

另一方面,正如 Adrian 在评论中所说,C++ 语言对这两种结构没有影响:

The C++ standard states that a missing condition makes the implied whileclause equivalent to while(true)and for ( for-init-statement condition opt ; expression opt )statement is equivalent to { for-init-statement while ( condition ) { statement expression ; } }

C++ 标准规定,缺少条件使隐含while子句等效于while(true)for ( for-init-statement condition opt ; expression opt )语句等效于{ for-init-statement while ( condition ) { statement expression ; } }