2

Recently while going through a c++ tutorial I encountered a for loop that looked like this:

for (;;){
   //Do stuff
}

Is this an infinite loop? Why would I use this rather that while(1)?

Deekor
  • 9,144
  • 16
  • 69
  • 121
  • 3
    Yes. Use the one you like better. – jrok Apr 16 '13 at 22:38
  • Yes. No reason to use it instead of `while(1)` or `while(true)`: matter of preference. But to my taste: `for (;;)` is ugly. – Alexander Shukaev Apr 16 '13 at 22:39
  • 4
    `for (;;)` is obviously better style. – john Apr 16 '13 at 22:39
  • It's better because it doesn't contain a magic constant. – Kerrek SB Apr 16 '13 at 22:40
  • @KerrekSB That's overly pedantic, heh. – jrok Apr 16 '13 at 22:41
  • Its not really infinite either. You either break or return or do a kill or turn the computer off sometime. – Lee Meador Apr 16 '13 at 22:42
  • 2
    @Kerrek SB: But it contains magic vampire canines. `(;;)` – Alexander Shukaev Apr 16 '13 at 22:42
  • 1
    @jrok: I don't know... `while (true)` insinuates that you're checking some condition, and I find that gratuitous. I don't have any conditions. I just want to loop. I find it harder to read out and reason about "while true" than about the `for` loop, which I can comfortably read as "for ever"... If anything, fewer keywords are needed for the `for` loop, and I guess simple problems should have simple solutions. – Kerrek SB Apr 16 '13 at 22:45
  • Just do `#define FOREVER for(;;)`, and then use as `FOREVER { ... }`, the war is over. – Alexander Shukaev Apr 16 '13 at 22:47
  • @Haroogan: Vampires aren't magic. Only ponies and dolphins are. And if anything it's a mutant smiley... – Kerrek SB Apr 16 '13 at 22:47
  • 2
    @LeeMeador I love your trolling on this question. Correct, these loops are not 'infinite'. For people like you, I usually say 'indefinite'. For the rest of us, we just accept the common meaning and get on with life. =) – paddy Apr 16 '13 at 22:49
  • 1
    @KerrekSB I have trouble imagining anyone that has to reason about `while(true)` or `for(;;)` when reading code. It reads "infinite loop" and then you move on. Considering the traffic this question gets everytime, though, it seems that's just me. – jrok Apr 16 '13 at 22:53
  • 1
    While I don't personally have any trouble understanding `for(;;)`, I'm flabbergasted by people who actually think it's easier to understand than `while(true)`. If you know how a while loop works normally, it seems braindead obvious why `while(true)` is an infinite loop. However, with the normal for loop operation, the expression between the first and second semi-colon is the condition. So with `for(;;)`, which has no expression there, why does it mean loop forever, instead of don't loop at all? Why is it even legal? Why does a non-existent expression equate to true? – Benjamin Lindley Apr 16 '13 at 23:46

3 Answers3

8

Yes, it's infinite. Traditionally, compilers would generate a warning when you use while(1), but not when you use for(;;). I don't know if this is still the case.

paddy
  • 60,864
  • 6
  • 61
  • 103
5

It's an infinite loop. More precisely, if the condition in a for is empty, it is considered true. As for while ( true ) vs. for (;;): historically for (;;) was the idiomatic form (used by Kernighan and Ritchie), perhaps partially because early C didn't have booleans. Using while ( 1 ) wouldn't pass code review anywhere I've worked. With booleans, while ( true ) definitely seems more intuitive than for (;;), but while ( 1 ) is confusing. But in pre-boolean times, everyone had a #define for true or TRUE or some such, so it's a weak argument. In the end, if you're an old C programmer, like me, who originally learned from Kernighan and Ritchie, you just instinctively use for (;;). Otherwise... it probably depends on where and from whom you learned C++.

Of course, when at work, you follow the house conventions, what ever they are.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
James Kanze
  • 150,581
  • 18
  • 184
  • 329
3

Is this an infinite loop?

Yes.

Why would I use this rather that while(1)?

Because of (bad, IMO) taste. By the way, I would go for while (true), if I really had to create an infinite loop.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • Real programmers use a `goto` for these sorts of loops and another `goto` (or `return`) to exit them. :) – Lee Meador Apr 16 '13 at 22:40
  • 2
    @LeeMeador: Real programmers don't use `goto` ;) – Andy Prowl Apr 16 '13 at 22:41
  • 3
    +1 for wisdom. -1 for the missing sense of humor. – Lee Meador Apr 16 '13 at 22:43
  • 1
    @LeeMeador: Sometimes sarcasm is hard to spot on SO ;) – Andy Prowl Apr 16 '13 at 22:44
  • 1
    Stupid and Sarcastic both start with an "S" – Lee Meador Apr 16 '13 at 22:46
  • So, what is so bad about using `goto` reasonably? Every loop uses it under the hood anyway... – dtech Apr 16 '13 at 22:47
  • @ddriver: All right, is this sarcasm again? :) – Andy Prowl Apr 16 '13 at 22:48
  • @AndyProwl - I am being 100% serious. If it was that bad it would be deprecated or something... – dtech Apr 16 '13 at 22:49
  • I mean a lot of stuff CAN be harmful, but it is only `goto` that gets all the bashing. – dtech Apr 16 '13 at 22:50
  • @ddriver `goto` is not exception-safe. – paddy Apr 16 '13 at 22:51
  • @ddriver: design-wise, `goto` is deprecated [since 1968](http://en.wikipedia.org/wiki/Considered_harmful) ;) – Andy Prowl Apr 16 '13 at 22:52
  • @AndyProwl The reference is to an article: "Real Programmers Don't Eat Quiche". Something like 30 years ago, in _Datamation_, I think. A beautiful piece of irony. – James Kanze Apr 16 '13 at 22:52
  • 1
    @paddy, `goto` is as exception safe as anything else. The reason we don't use it is because it quickly renders programs unmaintainable. (Note that the same thing holds for `goto` under a different name. Like `break` in a loop, or a return other than at the end of the function.) – James Kanze Apr 16 '13 at 22:53
  • @AndyProwl - I remember my first steps with computers only about 25 years ago, and IIRC `goto` was heavily used in the Basic implementation running on Apple 2. And that was like 2 decades after 1968. – dtech Apr 16 '13 at 22:55
  • @ddriver: Deep changes are slow. People started to say "don't use raw pointers for memory management" like 15 years ago (maybe earlier?), and it's still being done. It takes time, but `goto` is definitely out now – Andy Prowl Apr 16 '13 at 22:56
  • @AndyProwl Not everyone bought into this principle immediately. It didn't become universally accepted until 10 or 15 years later. K&R don't use it much, if ever, but they didn't feel justified at that time in banning it (imposing their views on the world). Since then, of course, practice _has_ shown that Dijkstra was right. – James Kanze Apr 16 '13 at 22:57
  • @AndyProwl That's not a good comparison. Those of us who started using smart pointers 15 years ago have gradually realized that they don't really solve very many problems, and that used without thinking, they cause more problems than they solve. – James Kanze Apr 16 '13 at 22:59
  • @AndyProwl - so, you mean that raw pointers are gonna follow in the footsteps of `goto`? I think raw pointers will always have their area of application, as well as `goto`. The danger, after all, is not in the language feature, but in the developer. I see no problem of using those "dangerous" features reasonably, in situations they can't backfire. – dtech Apr 16 '13 at 23:00
  • @ddriver The problem isn't `goto` per se; it is how it is used. Dijkstra's point was that there are very few valid constructs, and that it is better to support them directly in the language (like all modern languages do) than to have to implement them manually with `goto` (which is open to all kinds of abuse). When you have the constructs (e.g. `while`, `if`, blocks, etc.), then you shouldn't use `goto`. When you don't have them (as in most early Basic), then you have to simulate them with `goto`. – James Kanze Apr 16 '13 at 23:01
  • @JamesKanze: Let's not start an off-topic debate here. My personal opinion is that using raw pointers for manual memory management is harmful and should be avoided. You're right when you say that when used without thinking, they create more troubles than they solve; but then again, using something without thinking is not a very good approach to programming - and not only programming. – Andy Prowl Apr 16 '13 at 23:04
  • @AndyProwl I think the key is "memory management". I don't use pointers for memory management (in general). I use pointers for navigation. But there are exceptions. – James Kanze Apr 17 '13 at 09:28
  • @JamesKanze: Indeed, that was my point. Raw pointers should not be used for memory management, like `goto` should not be used for flow control. The latter guideline is now pretty much accepted, while the former is still quite often violated - it takes time, the guideline is still relatively young. – Andy Prowl Apr 17 '13 at 09:44
  • @AndyProwl Raw pointers can't be used for memory management, since that's not part of their semantics. So no one does use raw pointers for memory management. Smart pointers are a cleaner alternative to `try...catch` blocks, but most memory management doesn't use `try...catch` blocks either, and smart pointers are rarely applicable to management of object lifetimes. – James Kanze Apr 17 '13 at 11:28
  • @JamesKanze: Raw pointers *should not* be used for memory management for the reason you say ("since that's not part of their semantics"), but they are (unfortunately) being used for memory management with `new` and `delete` pretty often - just look how many newbies come to SO for troubleshooting related problems - in spite of their semantics. That's bad. Like using `goto` is bad. Smart pointers are to raw memory management what `for` and `while` cycles are to `goto`. – Andy Prowl Apr 17 '13 at 11:37
  • @AndyProwl `new` and `delete` are part of any memory management, but raw pointers aren't. What you seem to be saying is that a lot of people aren't managing memory at all. I'm not sure. Perhaps a lot here, but not in the places I've been. Generally, memory management is subsumed by lifetime management (and smart pointers play a very small role there, if any). Smart pointers are _not_ the usual solution for lifetime management. – James Kanze Apr 17 '13 at 11:44
  • @JamesKanze: As I wrote a few hours ago, "*Let's not start an off-topic debate here*" :) – Andy Prowl Apr 17 '13 at 12:02