Infinite loops - top or bottom? [closed]
Asked Answered
Y

18

9

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;
Yogini answered 22/10, 2008 at 1:16 Comment(3)
Related question: https://mcmap.net/q/18819/-why-use-infinite-loopsYogini
possible duplicate of Endless loop in C/C++Ashlan
It's popular in some circles to do #define ever ;; just so you can then do for(ever){ … }.Householder
D
36
while(true) {}

It seems to convey the meaning of the loop most effectively.

Diegodiehard answered 22/10, 2008 at 1:16 Comment(0)
V
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 */ 
}
Vicennial answered 22/10, 2008 at 1:16 Comment(0)
T
13
#define forever for(;;)

forever {
    /*stuff*/
}
Theodolite answered 22/10, 2008 at 1:16 Comment(0)
F
7
while(1)
{
//do it 
}

That's how I roll.

Fred answered 22/10, 2008 at 1:16 Comment(0)
M
6

I like to use the for(;;) approach because the MSVC++ compiler complains about while loop approach:

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

  for(;;)
  {
  }
}
Meanwhile answered 22/10, 2008 at 1:16 Comment(1)
I like that it complains - gives a cue that, yes, it is indeed an infinite loop.Fred
W
5

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.

Whortleberry answered 22/10, 2008 at 1:16 Comment(2)
'while (true)' could be confusing as 'for (;;)' if you think about it too long. Under what circumstances does the while exit? Maybe a '#define forever while(true)' would give us a more English keyword.Parvenu
My thoughts exactly, Adam. for (;;) is cool in a dirty C kind of way, but I wouldn't bring it out in public.Wendeline
I
3

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.

Ibnsina answered 22/10, 2008 at 1:16 Comment(0)
E
3

Infinite tail-recursion ;)

It's somewhat compiler-dependant...

Embryologist answered 22/10, 2008 at 1:16 Comment(1)
Only if you use a niche compiler. The most important ones do it as optimizations.Strother
T
3

I usually use for(;;) { } which I always think of as "for-ever".

Some languages offer a repeat { } construct which will natively loop forever. I find the for(;;) { } construct visually the most similar to this because it is so different from the normal for() construct. This is an important attribute for an infinite loop that while(1) { } doesn't really have.

Teat answered 22/10, 2008 at 1:16 Comment(4)
Which languages have a repeat keyword?Mcdaniel
Dunno, but Perl 6 has loop { } for the same idea.Musicianship
Icon, Logo and Rexx are the first that come to mind. There are others.Teat
R has repeat { } too.Fee
A
3
10 some l33t code
20 goto 10
Anorexia answered 22/10, 2008 at 1:16 Comment(0)
U
2

Let the flaming begin...

If the loop is a true infinite loop (i.e. there is no break condition -- only an external event can terminate the thread's/process' execution), then I actually prefer the label and goto. Here's why:

First, the use of while, for, and do ... while, all imply that the loop might terminate. Even if the terminating condition is never achievable, the syntactical meaning of these constructs is that there is some termination condition.

Second, using a loop construct introduces an extra level of indentation. I hate indentation that's not necessary. It wastes valuable columnar real-estate.

Third, the only true infinite loop is the one that unconditionally jumps back to the beginning of the loop. Only goto fits that purpose exactly.

The truth is I don't really care that much about it. They all get the job done and most will result in the exact same assembly instructions anyway. However, the assembly that's generated will in all probability be an unconditional jump (if you're optimizer is worth a damn), which maps directly to which C construct, kids? That's right... your old friend goto.

Underprop answered 22/10, 2008 at 1:16 Comment(0)
C
1
for(;;);

Filler text.

Coz answered 22/10, 2008 at 1:16 Comment(0)
D
1

for (;;) is what I usually see.

Demeter answered 22/10, 2008 at 1:16 Comment(0)
S
1

offtopic: if you think about what you are trying to express, you usually won't need an infinite loop.

Susann answered 22/10, 2008 at 1:16 Comment(2)
Not necessarily. I've run across many cases where an infinite loop (with or without a break) is the cleanest way to express something.Cockaigne
Ya, I have observed the same thing. I pop in an infinite loop, and morph the code to meet the unit test. Before I can blink, I see that it can be turned into a normal loop construct.Vicennial
R
1

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).

Religiosity answered 22/10, 2008 at 1:16 Comment(1)
If other people are confused by that syntax, they need to learn the programming language better. It's hard to imagine someone being caught off-guard by something in the core language syntax...Musicianship
F
0

I now prefer the "for (;;)" idiom because it seems to 'stick out' more. I used to use the "while (true)" idiom because I thought it expressed intent better, but I've switched over because I think the "for (;;)" idiom is well known enough to adequately express intent as well as I believe it's better by being more visible.

Kind of like how Stroustrup made the new casts in C++ purposefully ugly - so they stick out.

Frig answered 22/10, 2008 at 1:16 Comment(0)
K
0

I usually use while() {}, but after learning that for(;;) {} isn't some sort of crazy invalid syntax, I'll be sure to use the more unique option.

Differentiates infinite loops from actual conditionals, you see.

Kyat answered 22/10, 2008 at 1:16 Comment(0)
H
0

Infinite loops are a bad idea, but in practice that doesn't always hold up.

I prefer while(1) { } but make sure something within the loop can cause it to break out.

Halmstad answered 22/10, 2008 at 1:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.