无限循环 - 顶部还是底部?

Ces*_*arB 10 c c++ coding-style

本着问题的精神,你的循环测试是在顶部还是底部?:

你使用哪种风格进行无限循环,为什么?

  • 而(真){}
  • do {} while(true);
  • for(;;){}
  • label:...转到标签;

JPr*_*ers 36

while(true) {}
Run Code Online (Sandbox Code Playgroud)

它似乎最有效地传达了循环的含义.


Evi*_*ach 22

for (;;)
{
    /* No warnings are generated about constant value in the loop conditional
       plus it is easy to change when you realize you do need limits */ 
}
Run Code Online (Sandbox Code Playgroud)


dsm*_*dsm 12

#define forever for(;;)

forever {
    /*stuff*/
}
Run Code Online (Sandbox Code Playgroud)


Pau*_*han 7

while(1)
{
//do it 
}
Run Code Online (Sandbox Code Playgroud)

这就是我滚动的方式.


jus*_*sij 6

我喜欢使用for(;;)方法,因为MSVC++编译器抱怨 while循环方法:

void main()
{
  while(1) // test.cpp(5) : warning C4127: conditional expression is constant
  {
  }

  for(;;)
  {
  }
}
Run Code Online (Sandbox Code Playgroud)


Ada*_*eld 5

我更喜欢while(1)while(true)- 它是最清楚的. do { } while(true)好像不必要的混淆.同样,for(;;)可能会让以前从未见过的人感到困惑,而while(true)非常直观.而且绝对没有理由这样做label: ... goto label;,这更令人困惑.