术语“空循环”在 C 和 C++ 中究竟指的是什么?

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

What does the term "empty loop" refer to exactly in C and C++?

c++c

提问by vidit jain

Is it this kind of thing:

是不是这种东西:

for(;;)
 {
   statements;
 }

Or is it this:

或者是这个:

for(initialisation;condition;updation)
{
}

I am looking for answers with references to a variety of sources.

我正在寻找参考各种来源的答案。

回答by Bojan Komazec

Your first case (forwith empty expressions) is an infiniteloop and the second one (with empty body of the forstatement) is an emptyloop

您的第一个案例(带有空表达式的for)是一个无限循环,第二个案例(带有空的for语句体)是一个循环

回答by RoflcoptrException

In my environment it is like this:

在我的环境中是这样的:

for(;;) { statements; }

endless loop

无限循环

for(initialisation;condition;updation) { } 

empty loop

空循环

回答by darioo

Answer is context dependent.

答案取决于上下文。

If you mean an empty forloop, then

如果你的意思是一个空的for循环,那么

 for(;;)
 {
     statements;
 }

is such a thing.

是这么回事。

Although, the same thing can be achieved with a while loop:

虽然,同样的事情可以用一个 while 循环来实现:

while(true)
{
    statements;
}

and this isn't an "empty" loop. Both of these are infinite loops that you must break out of using breakinside of your loop.

这不是一个“空”循环。这两个都是无限循环,您必须break在循环内部使用它们。

On the other hand,

另一方面,

for(initialisation;condition;updation)
{
}

this is an "empty" loop that bascially does nothing, except perhaps update some variables that could be defined before the loop itself.

这是一个“空”循环,基本上什么都不做,除了可能更新一些可以在循环本身之前定义的变量。

回答by Mephane

An empty loop is a loop which has an empty body, e.g.

空循环是具有空体的循环,例如

for(int i = 0; i < 10; ++i) {}
while(cin) {}

(note that the second example here also happens to be endless)

(注意这里的第二个例子也恰好是无穷无尽的)

There are cases where these are useful, for example when a function has a desired side-effect and returns its success, and should repeated until unsuccessful, for example to read the last line in a file:

在某些情况下,这些很有用,例如,当一个函数具有所需的副作用并返回其成功时,应该重复直到不成功,例如读取文件中的最后一行:

std::string getLastLine(std::string filename)
{
  std::ifstream in(filename.c_str());
  if(!in)
    return "";

  std::string line;
  while(std::getline(in, line)); // empty loop, the operation returns the condition
  return line;
}

回答by VickyP

for(;;)
 {
   statements;
 }

is endless loop because there is redundant value/Grabage value which makes the loop true

是无限循环,因为有冗余值/Grabage 值使循环为真

for(initialisation;condition;updation)
 {
   body;
 }

is just syntax for for loop (educational purpose use)

只是 for 循环的语法(教育用途)

回答by Samet Atdag

It equals to that:

它等于:

while (true) {
  statements;
}

Infinite for loop is a loop that works until something else stops it.

无限 for 循环是一个循环,直到其他东西停止它为止。