Jump to content
  • Advertisement
Sign in to follow this  
Cwiddy

Infinite Loops in c++

This topic is 4092 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is probably a very simple question but I am unsure if there is a reason or just personal preference. In a lot of libraries I tend to see infinite loops written as for(;;) rather then while(true). Is there any difference once a compiler has it's way with the code? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Nope, no difference. However, VC 2003 and VC 2005 give a warning about while(true) on the highest warning level (Warning: Conditional expression is constant).

Share this post


Link to post
Share on other sites
The binary result is the same in both cases on every professional compiler available. One might argue that for(;;) avoids warnings related to constant conditions, while another might argue that while(true) is closer to the intent. Ultimately, the number of infinite loops in any program is small enough that this is an utterly pointless issue. I stick to whatever the team habits are, and use for(;;) in my own code for no reason but my own artistic preference.

Share this post


Link to post
Share on other sites
Quote:
Original post by Simian Man
I had a professor who did the following:

#define ever (;;)


for ever
{

// ...

}


I like it but, wouldn't use it in my own code.


Because, one day you'll do:

int ever = 20;

and spend an hour or two banging your head against the wall trying to figure out why it doesn't compile. Ah, the wonders of the preprocessor.

Skizz

Share this post


Link to post
Share on other sites
Quote:
Original post by Simian Man
I had a professor who did the following:

#define ever (;;)


for ever
{

// ...

}


I like it but, wouldn't use it in my own code.


I never use infinite loops, but now you showed me this I think I will!

R O F L

Share this post


Link to post
Share on other sites
Generally, I fail to see the use of infinite loops.

You still need a way to stop it eventually, if you're hoping for a clean exit.

And since infinite loops stop program execution after it, they'll likely operate in a thread.

So the general approach I use is:

bool running = true;

...

while (running) {
}



One could argue that exiting from a loop may be done using break, but that involves a condition test as well.

For algorithms, I'm strongly opposed to infinite loops. You either know when you're done, or you don't. The later obviously being non-deterministic, there-by undesirable choice.

Another approach I might use would be:

bool doTheStuff() {
// foo

return are_we_out_of_stuff;
}

...

while ( doTheStuff() ) ;



Both of these to me make it much clearer when, how and why we are exiting.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!