Jump to content
  • Advertisement
Sign in to follow this  
PhoenixAdmin

(For)ever loop

This topic is 4035 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 how you can make a loop go on forever. for(;;) forever loops are useful only when you have another loop that is nested between it. The number of nested loops doesn't matter. Example: for(;;){ while(condition){ do something } } But you want to end the loop or do the loop over again. You use the break; or continue; Example: for(;;){ while(condition){ do something continue; } break; } While you don't have to use the continue; statment you should always use the break; in a forever loop. Other alternatives would be abort;

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by PhoenixAdmin
This is how you can make a loop go on forever.

for(;;)
Or while(true), which IMO is nicer.

Quote:
forever loops are useful only when you have another loop that is nested between it.
Or.. when you want to make a loop go on for ever.

Quote:
Other alternatives would be abort;

abort isn't a keyword.

Dude, seriously... we appreciate your enthusiasm, but maybe it would be better to leave the teaching to the guys with the many years of experience as professional coders?

Share this post


Link to post
Share on other sites
Quote:
Original post by PhoenixAdmin
Lol okay I thought abort; was I never used it before though.
There is a function called abort() in the standard C library. It terminates a program immediately (like doing 'End Process' from Task Manager). It's probably not what you want for breaking out of a loop.

Quote:
O and yes but it says. For loops not while or other loops.
Doesn't really matter. At the end of the day, it's not about what the code is - it's about what the code means.

Share this post


Link to post
Share on other sites
the traditional way to break out of an intentionally infinite(ish) loop is to use a conditional of some kind (IE: if/switch) to detect conditions for terminating the loop, then simply break; the loop.

Share this post


Link to post
Share on other sites
Quote:

forever loops are useful only when you have another loop that is nested between it.

This isnt true. I think you are referring to "a way to break from the loop";
but even that isnt always the case.

Two examples...

The simple game loop normally will use an infinity loop, and break out
only when its shutting down.

An infinity loop as in for (;;); can be usefull for simulating
a hlt or jmp $ instructions.

In addition to for(;;), you can also use while(true) (as already suggested)
and while (1).

Quote:

The number of nested loops doesn't matter.

Compiliers have restrictions on the number in which you can nest.

Share this post


Link to post
Share on other sites
Oh good lord...

Quote:
Original post by medevilenemy
the traditional way to break out of an intentionally infinite(ish) loop is
No, there is no "traditional way" of breaking out of an infinite loop. Or a loop of any kind for that matter. This is because it depends very much on the situation in which your loop resides.

Quote:
Original post by Crypter
Quote:

The number of nested loops doesn't matter.

Compiliers have restrictions on the number in which you can nest.

Officially C++ doesn't have this restriction, though you're right in that compilers do. It's a large number though - Visual C++ 2005 limits you to 128 nesting levels (error C1061).

Share this post


Link to post
Share on other sites
I didn't mean traditional as in conventions or rules, I just meant that it is a very common old method.

in any case, lets just say that infinite loops are possible, practical in some circumstances, and are relatively easy to terminate when they are no longer needed.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!