[c#] Which is the correct C# infinite loop, for (;;) or while (true)?

Back in my C/C++ days, coding an "infinite loop" as

while (true)

felt more natural and seemed more obvious to me as opposed to

for (;;)

An encounter with PC-lint in the late 1980's and subsequent best practices discussions broke me of this habit. I have since coded the loops using the for control statement. Today, for the first time in a long while, and perhaps my first need for an infinite loop as a C# developer, I am facing the same situation. Is one of them correct and the other not?

This question is related to c# infinite-loop

The answer is


The C# compiler will transform both

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

and

while (true)
{
    // ...
}

into

{
    :label
    // ...
    goto label;
}

The CIL for both is the same. Most people find while(true) to be easier to read and understand. for(;;) is rather cryptic.

Source:

I messed a little more with .NET Reflector, and I compiled both loops with the "Optimize Code" on in Visual Studio.

Both loops compile into (with .NET Reflector):

Label_0000:
    goto Label_0000;

Raptors should attack soon.


I personally prefer the for (;;) idiom (coming from a C/C++ point of view). While I agree that the while (true) is more readable in a sense (and it's what I used way back when even in C/C++), I've turned to using the for idiom because:

  • it stands out

I think the fact that a loop doesn't terminate (in a normal fashion) is worth 'calling out', and I think that the for (;;) does this a bit more.


I think that this may be easier to read and is definitely the standard for use in C#:

while(true)
{
   //Do My Loop Stuff
}

Personally, I have always preferred for(;;) precisely because it has no condition (as opposed to while (true) which has an always-true one). However, this is really a very minor style point, which I don't feel is worth arguing about either way. I've yet to see a C# style guideline that mandated or forbade either approach.


The only reason I'd say for(;;) is due the CodeDom limitations (while loops can't be declared using CodeDom and for loops are seen as the more general form as an iteration loop).

This is a pretty loose reason to choose this other than the fact that the for loop implementation can be used both for normal code and CodeDom generated code. That is, it can be more standard.

As a note, you can use code snippets to create a while loop, but the whole loop would need to be a snippet...


Gasp, use:

while (!false)
{

}

OR as jsight pointed out, you may want to be doubly sure:

while (!false && true)
{
}

Before people yell at me, it's all the same CIL code, I checked :)


In terms of code readability while(true) in whatever language I feel makes more sense. In terms of how the computer sees it there really shouldn't be any difference in today's society of very efficient compilers and interpreters.

If there is any performance difference to be had I'm sure the translation to MSIL will optimise away. You could check that if you really wanted to.


If you're code-golfing, I would suggest for(;;). Beyond that, while(true) has the same meaning and seems more intuitive. At any rate, most coders will likely understand both variations, so it doesn't really matter. Use what's most comfortable.


Any expression that always returns true should be OK for while loop.

Example:

1==1 //Just an example for the text stated before 
true

To rehash a couple of old jokes:

  1. Don't use "for (;;) {}" — it makes the statement cry.
  2. Unless, of course, you "#define EVER ;;".

I prefer slightly more "literate" code. I'm much more likely to do something like this in practice:

bool shouldContinue = true;
while (shouldContinue)
{
    // ...

    shouldContinue = CheckSomething();
}

Both of them have the same function, but people generally prefer while(true). It feels easy to read and understand...


It should be while(true) not while(1), so while(1) is incorrect in C#, yes ;)


The original K&R book for C, from which C# can trace its ancestry, recommended

for (;;) ...

for infinite loops. It's unambiguous, easy to read, and has a long and noble history behind it.

Addendum (Feb 2017)

Of course, if you think that this looping form (or any other form) is too cryptic, you can always just add a comment.

// Infinite loop
for (;;)
    ...

Or:

for (;;)    // Loop forever
    ...

In those situations where I needed a true infinite loop, I've always used

while(true) {...}

It seems to express intent better than an empty for statement.


Even I also say the below one is better :)

while(true)
{

}

I think while (true) is a bit more readable.


If you want to go old school, goto is still supported in C#:

STARTOVER:  
    //Do something
    goto STARTOVER;

For a truly infinite loop, this is the go-to command. =)


Alternatively one could say having an infinite loop is normally bad practice anyway, since it needs an exit condition unless the app really runs forever. However, if this is for a cruise missile I will accept an explicit exit condition might not be required.

Though I do like this one:

for (float f = 16777216f; f < 16777217f; f++) { }