2

Possible Duplicate:
What does “for(;;)” do in C#?

what does

for (; ; )
{
  // do something
}

mean in c#?

Isn't there supposed to be (initializer, condition, iterator) ?

I saw an example in a book that uses nothing inside the contents of the for loop.

Community
  • 1
  • 1
Maya
  • 7,053
  • 11
  • 42
  • 53

5 Answers5

9

That would create an unconditional (infinite) loop which has no initializer or iterator.

same as

while(true)
{
...
}

You'll have to use break; to get out of the loop.

Bala R
  • 107,317
  • 23
  • 199
  • 210
1

At warning level 4 compilers complain about conditional expressions that always evaluate to true, such as while(1) or while(true).

So a common way to suppress the compiler warnings for that is to use for (;;) blocks.

(Im not saying its a good practice), but if you have to create code that compiles at warning level 4 its a habit to use for (;;) instead of while (true).

Ivan Bohannon
  • 707
  • 6
  • 20
0

Its just a loop that will run continuously.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
0

Like the other answers say, it's a loop that runs continuously until you break it with break;

It's usefull if you want to break on a condition that you create within the loop but you have no access to yet at the definition of the loop.

Bazzz
  • 26,427
  • 12
  • 52
  • 69
0

Yes, that's how an infinite loop is defined using a for statement.

You could read that as:

for (bool run = true; run == true; run &= true)
{
    // do something
}

See what the C# Reference has to say about that

Gustavo Mori
  • 8,319
  • 3
  • 38
  • 52