[ACCEPTED]-What does "for(;;)" do in C#?-.net
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)
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).
You are correct. This is a common C# idiom 1 for an endless loop.
Correct. Note that the braces of a for loop 6 contain three parts:
- Initialization code
- A condition for continuing the loop
- 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.
Yes, It is an infinite loop.
0
If I recall correctly it's use over "while(true)", is 1 it more resembles "for(;;) //ever"
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.
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(;;)
Loop forever.
0
Yes, it's an infinite loop. Same idea/effect 1 as doing while(true) { ... }
Inifinite loop like saying
while (0<1)
0
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.
It doesn't have an end condition, so it 2 will loop forever until it find a break, as 1 you already guessed.
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.
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.
Yes! .
0
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
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.