Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# for loop help [duplicate]

Tags:

c#

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.

like image 940
Maya Avatar asked Jul 27 '26 07:07

Maya


2 Answers

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.

like image 76
Bala R Avatar answered Jul 28 '26 21:07

Bala R


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

like image 23
Ivan Bohannon Avatar answered Jul 28 '26 20:07

Ivan Bohannon