8

It was a little while since I last programmed and I have seem to forgotten if it's acceptable to use an empty "for loop" for creating an infinite loop?

for(;;)

Currently I use this method in a program to make it repeatedly ask the user to enter two numeric values one for each double variable in the program. The programs then calls a function and calculates a sum of these two pairs of numbers.

To terminate the program i have "if" statements that check if the user input value is zero, If the value is zero the program terminates using an "Return 0;" argument.

The program checks each user input value if it's zero directly after the value has been assigned to the variable.


So to the real question: Is this a correct way to make my program do what i described? Or is there a more/better/accepted way of programming this?

And secondly is there anything wrong with use the "Return 0" argument the way i did in this program?

If you thinks it's hard to understand what I'll wrote or meant please reply, and I will take more time to write everything.

Anonymous
  • 81
  • 1
  • 3
  • 1
    There are arguably more elegant ways (do-while), but there is certainly nothing wrong with your way as such. – Damon May 03 '11 at 10:00
  • FYI, that is an infinite for loop, not an empty one. An empty for loop would look like this `for (initialisation;condition;updation);` or this `for (initialisation;condition;updation){}` – Troyseph Feb 11 '15 at 16:48

9 Answers9

5

What you're doing is perfectly fine, and an idiomatic way of writing and exiting an infinite loop.

Erik
  • 88,732
  • 13
  • 198
  • 189
5

I always use while(true) for infinite loops

Matthias
  • 12,704
  • 13
  • 35
  • 56
2

This is valid, you can go ahead with your code.

Gaurav
  • 28,447
  • 8
  • 50
  • 80
  • `while(1)` will cause a compiler warning on most platforms, `for(;;)` will not. In this respect, `for(;;)` is better. – ildjarn May 03 '11 at 10:10
  • 1
    @ildjarn: "most platforms" being MSVC, or do EDG-based compilers warn for it too? – Steve Jessop May 03 '11 at 10:17
  • we can use this int flag = 1; while(flag==1){} – Gaurav May 03 '11 at 10:20
  • @Steve Jessop : Reasonable question, but I don't have *any* compiler handy on this computer to verify with. :-[ – ildjarn May 03 '11 at 10:25
  • 1
    @ildjarn: GCC has never given me a warning for using `while(true)`. – Mike Seymour May 03 '11 at 10:33
  • @Mike Seymour : Fair enough, though personally I think it *should*. I retract my "on most platforms" statement and revise that to "on MSVC". :-] – ildjarn May 03 '11 at 10:36
  • 1
    @ildjarn: and I think it shouldn't, because I don't think there's anything wrong with having a constant expression in a condition. I think it's pretty arbitrary that MSVC warns for `true` in a loop continuation, but not for an empty loop continuation condition. Especially in C++, since that constant expression might be the result of some fairly exciting template meta-program. If a compiler was so feeble that it wasn't going to omit the dead code when you do it in `if` then a warning might be justified for that, but we know MSVC *isn't* that feeble, to me that warning just enforces a style. – Steve Jessop May 03 '11 at 10:50
  • 1
    @Gaurav: that's only slightly inferior to `while(true)` (in that a reader has to look in the loop body to see whether `flag` is modified in there), but enough so that I would prefer `for(;;)` as a means of avoiding the warning. – Steve Jessop May 03 '11 at 10:51
  • @Steve : thanks for such a great clarification. thank you all guys. – Gaurav May 03 '11 at 10:53
  • @Steve Jessop : Personally, I think having constant expressions inside a condition is a bad code smell. I'm not advocating that it be an error, but if the compiler can point out dead code paths to the programmer (or maybe just a dumb oversight on the part of the programmer), then why not? (That's rhetorical by the way.) – ildjarn May 03 '11 at 11:02
  • 1
    @ildjarn - The problem with the warning is that the "constant" condition can choose the if-part for one template instantiation and the else-part for another. Is that an error? This makes it one of the ["silly warnings"](http://alfps.wordpress.com/the-no_sillywarnings_please-h-file/) I have disabled (and I can then also use `while(true)` as much as I like). – Bo Persson May 03 '11 at 11:14
  • @Bo Persson : An error? No. An indication of probable poor design? IMO, yes, hence why I like the warning. But apparently I'm in the minority on this one. ;-] – ildjarn May 03 '11 at 11:21
  • @ildjarn: I don't want my compiler to warn me about possible poor design, I only want it to warn me about dangerous code, but I do want as much analysis and as many warnings as possible to that end. What if the compiler thinks Singletons are great and I don't (or vice-versa)? Is Microsoft's opinion on code design necessarily any better than mine or yours? Maybe `/Wunopinionated` as an alternative to `/Wall` ;-) – Steve Jessop May 03 '11 at 12:22
  • @Steve Jessop : I don't see the real issue here; I like the warning, you don't, but it exists and can be disabled -- we're both happy. I'd rather have a warning available that I can disable than not have one and want it. What's the problem? – ildjarn May 03 '11 at 12:29
  • @ldjarn: the practical problem is that when I'm writing portable code, I pretty much have to adopt Microsoft's house style (and for that matter any style warned by any compiler in the world), because otherwise people using that compiler will see spurious warnings. Alternatively, when writing portable code I could include a lot of compiler-specific pragmas to disable warnings I don't like, or I could give instructions, "this doesn't compile cleanly on MSVC with `/Wall`", but the former is annoying for me and the latter is annoying for users of the code. – Steve Jessop May 03 '11 at 13:55
  • Which is why I'd ideally like a warning level that says, "I don't see any likely *faults* in this code, and I'll keep my opinions on your code design to myself". The same thing would be useful in other compilers: GCC also has some warnings that amount to style guides, and if it existed then this option would be the sensible warning level to use to compile pre-existing code that you do not wish to refactor to match house style and design guidelines. `/Wall` would still be suitable when writing new code. I recognize that my ideal ain't gonna happen, though... – Steve Jessop May 03 '11 at 14:01
2

for(;;) as well as while(1) both are acceptable. These are just conditional loops provided by the language and you can use them to have a infinite running loop as per your requirement.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
2

I've seen this in a few places:

#define forever for(;;)

forever {

}

Not sure I'd recommend it though.

hammar
  • 138,522
  • 17
  • 304
  • 385
1

Yes, it's totally acceptable. Once you have an exit condition (break or return) in a loop you can make the loop "infinite" in the loop statement - you just move the exit condition from the loop statement into the loop body. If that makes the program more readable you of course can do that.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
0

For an infinte loop for (;;) is fairly common practice. But if you do have a condition, such a non-zero user input, you could always have that check done in a while loop.

musaul
  • 2,341
  • 19
  • 26
0

You can also use while loop with condition to repeatedly request user to input.

while (condition) {
  ...
}

Instead of IF block to validation you can use the .

Pravin
  • 1,059
  • 8
  • 17
0

What you describe will work fine, but it is worth mentioning that certain strict coding standards (i.e. MISRA) would disapprove of using a return before the end of a function.

If your code is subject to such standards then you could use do-while loop with a suitable exit condition instead:

do {
   // get userinput
   if (userinput != '0')
   {
       // do stuff 
   }
} while (userinput != '0');
GrahamS
  • 9,980
  • 9
  • 49
  • 63