Did you know? (Infinite loops)

Cags

Well-known member
Joined
Feb 19, 2004
Messages
690
Location
Melton Mowbray, England
I was recently browsing through a book on C# 2.0 and came across an example of using a for loop to create an infinite loop. Im not sure why youd want to, incidently neither was the book, it just listed it incase you came across it. But has anyone ever come across this syntax.
Code:
for(;;)
{
    // do something
    // obviously youd want to check for an exit clause
}
// just for clarification purposes c# evaluates all the statements as true,
// thus producing the same outcome as this
while(true)
{
    // do something
    // obviously youd want to check for an exit clause
}
[edit]I used the code tags because the cs tags couldnt cope the for statement (it tried to load a picture). Also note its not C# 2.0 specific, it does work in 1.1.[/edit]
 
I would have to say that the while(true) form of the loop is much more common, though neither of the two are generally used by a reasonably experienced programmer. It has the makings of spaghetti code, not to far from here:
C#:
loopstart:           // Same as while(true) {
    goto loopend;    // Same as    break;
 
    goto loopstart;  // These two lines
loopend:             // are the same as }

P.S. Cags, the reason for the image link that appeared in the C# code is the ; ) in for( ;; ) (hence the name wink.gif). All you needed were some spaces. Look: for(;;).
 
Last edited by a moderator:
for(;; ) is a throwback from C/C++. Ive seen them on occasion...whenever you need a truly infinite loop for something -- which is very rare. I prefer while(true) just because its a lot more in your face, you dont have to think about as much, and harder to miss.
 
An "infinite" loop is really just a "hopefully not infinite" loop. Meaning, its created to be infinite but in the middle theres a break statement to get out. Its usefull in a number of cases. Some games were written that way, but mostly for test code. The most common example that comes to mind is code waiting on an external resource, such as data on a serial port or similar. You may have no idea how much data is coming so the loop just loops until something is done.

Those types of infinite loops COULD be written to not use break, but set a value that is checked in the for loop. The code would not be as efficient doing a check on every loop, hence the C++ style of infinite loops.

The more typical code snippet would look like this:
Code:
while(true)
{
    if (!GotData()) break;
    ProcessData();
}

The "better" loop does the check in the loop but may or may not be more readable. Also note that my implementation does the "if" test twice, once in the while and once as an "if". This may be able to be restructured but adds even more code (calling GotData before the loop and inside the loop and calling ProcessData inside the loop and then after).
Code:
bool keepGoing = true;
while(keepGoing)
{
    keepGoing = GotData();
    if (keepGoing)
    {
        ProcessData();
    }
}

-ner
 
Heres another example of a time youd want an infinite loop that Ive posted up here before. This is different from Nerseuss example in that it is assumed that the loop ends when the program is terminated by the user (closed).
 
Back
Top