tags:

views:

552

answers:

10

I found an empty for statement in an existing bit of code and I'm wondering what it does and is it "safe". It just feels wrong.

for(;;)
{
   //some if statements and a case statement
}

Thanks!

+2  A: 

It's valid syntax for an infinite loop. You need to "break;" out of it. This was popular back in the C++ days IIRC.

As far as being safe, you're right in feeling "wrong" about this. Usually there would be an "if" condition inside where you would decide if you continue or break the loop. If you don't verify all execution paths it could very well lead to an infinite loop. I would try and do this some other way.

enriquein
+16  A: 

This is one way of creating an infinite loop. It's a regular for loop, but with empty initialization, condition, and increment expressions. Because the condition expression is a no-op, the loop never exits. It's perfectly "safe" assuming it has a terminating condition (a break or return statement [or even a goto, I suppose]) somewhere.

Personally, I prefer to write infinite loops with whiles:

while (true) 
{  
    //some statements and a case statement
}

(because for is for iteration and while is for repetition).

However, after reading this question (linked by @jailf), I now prefer while (42) { ... }.

Seth
I would use whiles just to enhance code readability
Neil N
I consider `for(;;)` more readable. :) If there is no condition for my loop, why should I write one (such as `true`)? ( `for(;;)` explicitly tells the reader "there is no terminating condition for this loop", which I consider as readable as it gets.
jalf
It is an interesting bit of trivia to ask students and novices if a totally empty boolean expression is true or false. Most of us would consider "empty" to be most analogous to 0/false. But C considers empty to be true, as shown by this expression.
abelenky
@abelenky: C# doesn't consider empty to be true: there's no such thing as an empty boolean expression in C#. The spec special-cases the situation where the `for` condition is missing, so there'll *either* be a valid boolean expression *or* nothing - two distinct scenarios.
LukeH
@abelenky: C doesn't consider empty to be true as can be shown by trying `while()`
Ramon Marco Navarro
+5  A: 

It's equivalent as having an infinite loop:

while (true) {

}

It's safe. You need to provide an external exit mechanism though. I.E., with a break within the for loop.

Pablo Santa Cruz
+1  A: 

Were there any break, return, or throw statements? That‘s the only way out. Is it safe? It depends if you feel safe inside an infinite loop. Some applications need one.

jleedev
...or calls which may throw, they also break out. ;-)
Lucero
`goto` can also get you out of the loop although I wouldn't use it.
Tuzo
@Tuzo, yes, I never ever use it so I don't even think of this possibility. But you're right of course.
Lucero
@Lucero, I know what you mean -- it's like it's almost banished from my mind.
Tuzo
+2  A: 

It's sometimes called a "forever" loop, because that's what it does. Look for either a break; or return; statement inside the loop or for the loop to be wrapped in a try/catch block. Personally, I try avoid that kind of thing.

Joel Coehoorn
+3  A: 

All of the expressions of the for statement are optional; for example, the following statement is used to write an infinite loop:

> for (; ; ) {
>     // ... }

Taken from MSDN

npinti
+3  A: 

It's the same as while (true) { /**/ } ... infinite loop until break or return or similar occurs.

All it really "does" is look ugly IMO ;)

no
In C#, a `1` is not equivalent to the boolean `true`.
Lucero
good catch, fixed.
no
+3  A: 

This has been asked multiple times on SO. The best discussion on the topic is at the following link:

http://stackoverflow.com/questions/2611246/is-for-faster-than-while-true-if-not-why-do-people-use-it

Michael
+3  A: 

This is a common idiom for an indefinite or infinite loop. You purposely might have an indefinite loop if you are looking for a condition that is not finite at the beginning -- such as user input or the end of a file of unknown size. You might also see while(1) or while(true) for the same thing. It says 'do this thing { whatever } until there is no more...'

Inside that loop structure is probably a conditional and a break statement, such as:

for(;;)
{
    Console.Write("Enter your selection (1, 2, or 3): ");
    string s = Console.ReadLine();
    int n = Int32.Parse(s);

    switch (n)
    {
        case 1:
           Console.WriteLine("Current value is {0}", 1);
           break;
        case 2:
           Console.WriteLine("Current value is {0}", 2);
           break;
        case 3:
            Console.WriteLine("Current value is {0}", 3);
            break;
        default:
           Console.WriteLine("Sorry, invalid selection.");
           break;
        }
    if(n==1 || n==2 || n==3)
        break; // out of the for(;;) loop    

 }

The key whether is it "safe" or not is to figure out the logic of how you leave that loop, or your indefinite loop will become an unintended infinite loop and a bug.

More at the C# site for for: HERE

drewk
+1  A: 

This is very common on embedded systems without an operating system. If your program terminates, there is no underlying system to handel that. So mostly there's one huge infinite loop in which most of the operations are handled.

Juri Robl