Ces*_*arB 10 c c++ coding-style
本着问题的精神,你的循环测试是在顶部还是底部?:
你使用哪种风格进行无限循环,为什么?
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)
我喜欢使用for(;;)方法,因为MSVC++编译器抱怨 while循环方法:
void main()
{
while(1) // test.cpp(5) : warning C4127: conditional expression is constant
{
}
for(;;)
{
}
}
Run Code Online (Sandbox Code Playgroud)
我更喜欢while(1)或while(true)- 它是最清楚的. do { } while(true)好像不必要的混淆.同样,for(;;)可能会让以前从未见过的人感到困惑,而while(true)非常直观.而且绝对没有理由这样做label: ... goto label;,这更令人困惑.