Is there anything wrong with using an empty for loop?
Asked Answered
V

9

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.

Valrievalry answered 3/5, 2011 at 9:57 Comment(2)
There are arguably more elegant ways (do-while), but there is certainly nothing wrong with your way as such.Clearcut
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){}Coxa
C
5

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

Cubical answered 3/5, 2011 at 10:0 Comment(0)
A
5

I always use while(true) for infinite loops

Alcina answered 3/5, 2011 at 10:9 Comment(0)
D
2

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

Dickey answered 3/5, 2011 at 9:59 Comment(16)
while(1) will cause a compiler warning on most platforms, for(;;) will not. In this respect, for(;;) is better.Tanner
@ildjarn: "most platforms" being MSVC, or do EDG-based compilers warn for it too?Sideburns
we can use this int flag = 1; while(flag==1){}Dickey
@Steve Jessop : Reasonable question, but I don't have any compiler handy on this computer to verify with. :-[Tanner
@ildjarn: GCC has never given me a warning for using while(true).Curmudgeon
@Mike Seymour : Fair enough, though personally I think it should. I retract my "on most platforms" statement and revise that to "on MSVC". :-]Tanner
@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.Sideburns
@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.Sideburns
@Steve : thanks for such a great clarification. thank you all guys.Dickey
@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.)Tanner
@Tanner - 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" I have disabled (and I can then also use while(true) as much as I like).Glasswork
@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. ;-]Tanner
@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 ;-)Sideburns
@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?Tanner
@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.Sideburns
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...Sideburns
M
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.

Membrane answered 3/5, 2011 at 10:2 Comment(0)
T
2

I've seen this in a few places:

#define forever for(;;)

forever {

}

Not sure I'd recommend it though.

Tonyatonye answered 3/5, 2011 at 10:5 Comment(2)
It just means you have to go and look up the define :)Metallize
@Tonyatonye it isn't forever, suppose you used break or goto.Raye
J
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.

Jemy answered 3/5, 2011 at 9:59 Comment(0)
M
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.

Maretz answered 3/5, 2011 at 10:1 Comment(0)
S
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 .

Seismology answered 3/5, 2011 at 10:1 Comment(0)
F
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');
Flinn answered 3/5, 2011 at 11:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.