Sign in to follow this  

(For)ever loop

This topic is 3841 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
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
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
Quote:
Original post by PhoenixAdmin
Well, abort(); is a cheap way of getting out of a loop.

Yes, abort() will get you out of the loop. It will also terminate your application with no chance to clean up any resources.

If you want to ask questions to find out whether you've understood something properly, then go right ahead. But posting things written as if they're definitive information, when you clearly have no clue what you're talking about doesn't help anyone (and potentially it leads people who know even less than you down the wrong path).

John B

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnBSmall
Quote:
Original post by PhoenixAdmin
Well, abort(); is a cheap way of getting out of a loop.

Yes, abort() will get you out of the loop. It will also terminate your application with no chance to clean up any resources.

If you want to ask questions to find out whether you've understood something properly, then go right ahead. But posting things written as if they're definitive information, when you clearly have no clue what you're talking about doesn't help anyone (and potentially it leads people who know even less than you down the wrong path).

John B


Yes but its not that bad of mistake I mean look at it the only thing that is really wrong is the abort; I should have added a (). But I didn't say you should use it I just said its an alternative the other thing I did wrong was not describe the abort(); Other then that the for forever is okay and the part about the nesting was my own opinion.

Share this post


Link to post
Share on other sites

Technically another way of breaking out of a loop (Note: Bad Programming Practice):

while (1) {

if (bBreak)
goto done;
}
done: ;


>>Please dont ever ever do this. Just here to demenstrate another method[smile]

Quote:

Yes but its not that bad of mistake I mean look at it the only thing that is really wrong is the abort; I should have added a (). But I didn't say you should use it I just said its an alternative the other thing I did wrong was not describe the abort(); Other then that the for forever is okay and the part about the nesting was my own opinion.


abort() and exit() are very simular in functionality, and imo,
are horrible routines to use. One should never use them in
release builds (Debug builds, mabey)

It is simular to my goto example above--They both have more cons then
pros, and hence shouldnt be used.

Share this post


Link to post
Share on other sites
Quote:
Original post by Crypter

Technically another way of breaking out of a loop (Note: Bad Programming Practice):

while (1) {

if (bBreak)
goto done;
}
done: ;


>>Please dont ever ever do this. Just here to demenstrate another method[smile]

Quote:

Yes but its not that bad of mistake I mean look at it the only thing that is really wrong is the abort; I should have added a (). But I didn't say you should use it I just said its an alternative the other thing I did wrong was not describe the abort(); Other then that the for forever is okay and the part about the nesting was my own opinion.


abort() and exit() are very simular in functionality, and imo,
are horrible routines to use. One should never use them in
release builds (Debug builds, mabey)

It is simular to my goto example above--They both have more cons then
pros, and hence shouldnt be used.


Its used in error handling am I right. Only in the worst possible situations.

Share this post


Link to post
Share on other sites
Quote:

Its used in error handling am I right. Only in the worst possible situations.

You can, but most use assert() to provide more information to help them
with debugging.

Share this post


Link to post
Share on other sites
Personally, the only place I've found use for infinite loops recently were threads.
volatile bool running;

void run() {
try {
while (running) {
// do the stuff
}
} catch ( ... ) {
}
}



Elsewhere I simply fail to see the use, especially in algorithms. Infinite loop should be use just for that - cases where it needs to run infinitely.

I'm also generally against breaking out of a loop (unless required due to I/O or some other dependant mechanism). A loop, regardless of whether it's finite or infinite has one, clearly define stop condition. Use it.

Break and continue along with goto IMHO don't belong into OO. Or any modern development. There are some rare uses for them, but I don't feel they are worth the deviation from usual application flow. Some are fond of them though, and when properly indented, continue doesn't hurt that much. The obvious exception of break is within switch statement.

For, while, do should all use their predefined places to establish stop conditions.

Share this post


Link to post
Share on other sites
Quote:
Original post by Crypter
Quote:

Its used in error handling am I right. Only in the worst possible situations.

You can, but most use assert() to provide more information to help them
with debugging.

Well I guess there is alot of cheap and smart ways to get out of a loop.

Share this post


Link to post
Share on other sites
Quote:
Original post by PhoenixAdmin
Yes but its not that bad of mistake I mean look at it the only thing that is really wrong is the abort; I should have added a (). But I didn't say you should use it I just said its an alternative the other thing I did wrong was not describe the abort(); Other then that the for forever is okay and the part about the nesting was my own opinion.

No, it is that bad of a mistake, because abort(); and break; do completely different things.
abort(); is not an alternative to break;, because it doesn't just break out of the loop, it terminates the entire application.

All of that is mostly irrelevant though - the point is not that you don't know what abort() does, but that you're writing as if you do. That's bad, not for you, but for anyone reading the post who knows less than you. That is why I'm coming down so hard on you - because by posting misleading information, you could be messing up someone else's learning.

There are plenty of well written C++ tutorials on the web already. There are also plenty of badly written tutorials. Bad either because they're technically misleading (not necessarily inaccurate in the strictest sense, but still very misleading), or because they encourage dangerous coding practices, or just because they're written by people who can barely speak English (and I'm talking about the ones who can barely speak English even though they were born and raised in an English speaking country - I can forgive the ones written by people for whom English is a second or third language).

At the moment, this post fits into the bad category, because it is technically misleading (again, not necessarily inaccurate, but misleading).

John B

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
Personally, the only place I've found use for infinite loops recently were threads.
*** Source Snippet Removed ***



same actually, though i suppose any C++ game really needs a while(true) loop to process the game...though i don't like the whole for(;;) notation, especially since theres not a nice way to break the loop...whats wrong with

bool gameRunning = true;

while(gameRunning)
{
//render, physics, ai, game logic, whatever!
if(endGameRunningCondition == true) gameRunning = false;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Winegums
Quote:
Original post by Antheus
Personally, the only place I've found use for infinite loops recently were threads.
*** Source Snippet Removed ***



same actually, though i suppose any C++ game really needs a while(true) loop to process the game...though i don't like the whole for(;;) notation, especially since theres not a nice way to break the loop...whats wrong with

bool gameRunning = true;

while(gameRunning)
{
//render, physics, ai, game logic, whatever!
if(endGameRunningCondition == true) gameRunning = false;
}



while ( !endGameRunningCondition ) {
//
}

Share this post


Link to post
Share on other sites
Quote:
Original post by jjd
This thread has made baby Jesus cry.



for (;;) {
Jesus::Baby->getInstance()->cry();
}


Here, let me add a singleton for more tears.

Share this post


Link to post
Share on other sites

This topic is 3841 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this