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

C#Infinite Loop

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

while(true)
{

}

Is always what I've used and what I've seen others use for a loop that has to be broken manually.

Solution 3 - C#

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

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

Solution 4 - C#

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 :)

Solution 5 - C#

To rehash a couple of old jokes:

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

Solution 6 - C#

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. =)

Solution 7 - C#

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

Solution 8 - C#

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.

Solution 9 - C#

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.

Solution 10 - C#

The original http://en.wikipedia.org/wiki/The_C_Programming_Language_(book)">K&R</a> 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
    ...

Solution 11 - C#

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.

Solution 12 - C#

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

Solution 13 - C#

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++) { } 

Solution 14 - C#

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();
}

Solution 15 - C#

Even I also say the below one is better :)

while(true)
{

}

Solution 16 - C#

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.

Solution 17 - C#

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...

Solution 18 - C#

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.

Solution 19 - C#

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

Solution 20 - C#

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

Example:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBob KaufmanView Question on Stackoverflow
Solution 1 - C#Pierre-Alain VigeantView Answer on Stackoverflow
Solution 2 - C#Adam RobinsonView Answer on Stackoverflow
Solution 3 - C#RSolbergView Answer on Stackoverflow
Solution 4 - C#Allen RiceView Answer on Stackoverflow
Solution 5 - C#Ben BlankView Answer on Stackoverflow
Solution 6 - C#JohnFxView Answer on Stackoverflow
Solution 7 - C#MarplesoftView Answer on Stackoverflow
Solution 8 - C#Michael PetrottaView Answer on Stackoverflow
Solution 9 - C#Pavel MinaevView Answer on Stackoverflow
Solution 10 - C#David R TribbleView Answer on Stackoverflow
Solution 11 - C#Michael BurrView Answer on Stackoverflow
Solution 12 - C#JRLView Answer on Stackoverflow
Solution 13 - C#Chris ChilversView Answer on Stackoverflow
Solution 14 - C#bobbymcrView Answer on Stackoverflow
Solution 15 - C#anishMarokeyView Answer on Stackoverflow
Solution 16 - C#ChrisView Answer on Stackoverflow
Solution 17 - C#Mark SynowiecView Answer on Stackoverflow
Solution 18 - C#sobellianView Answer on Stackoverflow
Solution 19 - C#abhinavView Answer on Stackoverflow
Solution 20 - C#GeorgeView Answer on Stackoverflow