Infinite loops - top or bottom? [closed]

2019-01-07 23:02发布

In the spirit of questions like Do your loops test at the top or bottom?:

Which style do you use for an infinite loop, and why?

  • while (true) { }
  • do { } while (true);
  • for (;;) { }
  • label: ... goto label;

19条回答
劫难
2楼-- · 2019-01-07 23:19
while(1)
{
//do it 
}

That's how I roll.

查看更多
叛逆
3楼-- · 2019-01-07 23:19

I prefer while(1) or while(true) -- it's the clearest. do { } while(true) seems like needless obfuscation. Likewise, for(;;) can be confusing to people that have never seen it before, whereas while(true) is very intuitive. And there's absolutely no reason to do label: ... goto label;, it's just more confusing.

查看更多
够拽才男人
4楼-- · 2019-01-07 23:20
#define forever for(;;)

forever {
    /*stuff*/
}
查看更多
叼着烟拽天下
5楼-- · 2019-01-07 23:23

When writing code for myself I use for(;;). Other people tend to be confused by its syntax and so for code that other people must see/use, I use while(true).

查看更多
爷的心禁止访问
6楼-- · 2019-01-07 23:24
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 */ 
}
查看更多
冷血范
7楼-- · 2019-01-07 23:26

I use for (;;) in C-style languages and while true in languages that don't support that construct.

I learned the for (;;) method in K&R and it has always felt like idiomatic C to me.

查看更多
登录 后发表回答