Jump to content
  • Advertisement
Sign in to follow this  
Kalner

for and while inifite loops

This topic is 2116 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

So I noticed that both for and while loops have a infinite loop option, is there any difference between them? 

Share this post


Link to post
Share on other sites
Advertisement

Assuming you mean while (true) vs for (;;) vs do { } while (true), no, there's no technical difference.
 
However, I've seem some compilers generate a warning* for while (true) and do { } while (true) but not generate a warning for for (;;), in which case for (;;) may be preferred to avoid a warning.
*For example, see this question.

Edit: I just realized this question was tagged Java and was assuming C or C++ when I was talking about the warning stuff. It's the same in Java as it is in C and C++ in that there's no real technical difference between these infinite looping methods, but the compiler warning issue I talked about isn't relevant in Java.

Edited by Cornstalks

Share this post


Link to post
Share on other sites
Didn't know about the compiler warning, nor have I experienced it myself, but that's good to know. I always went with while(true) because for(;;) didn't come naturally to me, but I'll probably switch it up to for(;;) now.

Share this post


Link to post
Share on other sites

I don't believe there's any performance difference between any flavors of the infinite loops but my personal preference is always to use a while(someValue == true).  This allows for the loop to be cancelled from within itself or from outside of the loop in say another thread.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!