[ACCEPTED]-What does "for(;;)" do in C#?-.net

Accepted answer
Score: 52

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

Yes. It 4 loops forever.


Also note the comment by Andrew 3 Coleson:

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

Score: 22

Yes.

In a for if nothing is provided:

  • The initialisation does nothing.
  • The condition is always true
  • The count statement does nothing

It is 1 equivalent to while(true).

Score: 17

You are correct. This is a common C# idiom 1 for an endless loop.

Score: 13

Correct. Note that the braces of a for loop 6 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 5 are empty, so there is nothing done to initialize 4 the loop, there is no condition to keep 3 it running (i.e. it will run indefinitely) and 2 nothing that gets executed for each iteration 1 except the loop's content.

Score: 12

Yes, It is an infinite loop.

0

Score: 11

If I recall correctly it's use over "while(true)", is 1 it more resembles "for(;;) //ever"

Score: 10

Yes, it's an endless loop, just like while(true).

It's 3 the slightly preferred convention, probably 2 because it's shorter. There's no efficiency 1 difference at all.

Score: 10

Take a look at a for loop.

for ( initialization ; condition ; increment  )

1) initialization - set a counter 5 variable here
2) condition - keep looping until the 4 counter variable meets the condition
3) increment - increment 3 the counter

If there is no condition, a loop 2 will go on forever. If it does such, then 1 there is no need for a counter. Therefore

for(;;)
Score: 9

Loop forever.

0

Score: 8

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

Score: 8

Inifinite loop like saying

while (0<1)

0

Score: 6

To be precise, any for loop without anything 3 between the semicolons will loop forever 2 (until terminated by some other means), because 1 it has no defined invariant.

Score: 4

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

Score: 4

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

for (; ;)

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

Score: 4

Yes, it loops forever. But the reason why 4 you should use

for(;;)

instead of

while(true)

is that

while(true)

will give 3 you a compiler warning "conditional expression constant", while the for-loop 2 does not. At least you'll get such a compiler 1 warning in the highest warning level.

Score: 2

Yes! .

0

Score: 1

Often used in embedded programming.

-setup 3 interrupts and timers. -then loop forever.

When 2 an interrupt or timer occurs that will be 1 handled.

More Related questions