Alex Rivera | Logout

What does "for(;;)" do in C#?

Asked 2009-06-11T17:22:30.423
20

I've seen this on occasion in books I've read. But I've found no explanation.

for (;;)
{
  // Do some stuff.
}

Is it kind of like "while(true)"? Basically an endless loop for polling or something? Basically something you'd do until you intentionally break the loop?

Edit
Report

5 Answers

53

Is it kind of like "while(true)"?

Yes. It loops forever.


Also note the comment by Andrew Coleson:

Languages like C don't have built-in boolean primitives, so some people prefer for(;;) over while(1)

answered 2009-06-11T17:23:19.410
14

Correct. Note that the braces of a for loop contain three parts:

  1. Initialization code
  2. A condition for continuing the loop
  3. Something that gets executed for each loop iteration

With for(;;), all of these are empty, so there is nothing done to initialize the loop, there is no condition to keep it running (i.e. it will run indefinitely) and nothing that gets executed for each iteration except the loop's content.

answered 2009-06-11T17:26:22.863
8

Yes, it's an infinite loop. Same idea/effect as doing while(true) { ... }

answered 2009-06-11T17:23:38.050
4

It doesn't have an end condition, so it will loop forever until it find a break, as you already guessed.

answered 2009-06-11T17:35:06.963
4

I might also add that it looks like 2 smiley faces winking at you

for (; ;)

maybe that's why some people like to use it.

answered 2009-06-11T19:08:19.483

Your Answer