tags:

views:

1166

answers:

17

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?

+41  A: 

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)

Joel Coehoorn
I don't get why this got 22 votes, with other similar answers.
Dmitri Farkov
It was right, and it was first. It happens. I didn't earn any rep for it- was already capped today.
Joel Coehoorn
That's an amazing number of votes!
RichardOD
One more vote and I get a silver badge, though. An accepted answer would earn another one, but personally my choice for accepted answer would be Andrew Colson's comment to the question. If this does end up as accepted and he doesn't answer I'll likely update my post to include the comment (with attribution).
Joel Coehoorn
And to put it into perspective, _this_ is an amazing number of votes: http://stackoverflow.com/questions/84556/whats-your-favorite-programmer-cartoon/84629#84629
Joel Coehoorn
You know, I also don't get why this got so many votes; I think there's a "fame effect", that people upvote answers over other answers that are very similar when a user has a really high rep.
McWafflestix
Maybe, but more likely it's an easy question and so everybody checks it, and this answer was easily verifiable and already at the top of the list. Trust me when I say everything I write doesn't get voted up like this.
Joel Coehoorn
+1 Joel, cuz U hit the button first! ;)
IAmAN00B
Joel is correct, I've even seen posts where that happens to him. It's... weird.
dss539
Yes, there might be a bit of a "fame effect" (I'm pretty sure people favor Jon Skeet's posts over others saying the same thing ;)But a much more common one is simply "upvote the post at the top if it looks correct". That means the first answer initially gets favored, and is likely to keep getting most of the votes. Don't worry about it, it's not a big deal
jalf
Btw, even in languages that do have boolean primitives, for(;;) may still be preferable over while(true). There is less chance of it being a typo, so it is more explicit about being intended to be an infinite loop. And a more practical reason is that some compilers may emit warnings for while(true). :)
jalf
+8  A: 

Loop forever.

Otávio Décio
+17  A: 

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

Brandon E Taylor
+12  A: 

Yes, It is an infinite loop.

ichiban
+8  A: 

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

matt b
+19  A: 

Yes.

In a for if nothing is provided:

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

It is equivalent to while(true).

Coincoin
Or is it that the condition is never false?
Even Mien
@theOtherScott, I think you're right.
Pim Jager
+8  A: 

Inifinite loop like saying

while (0<1)
TStamper
"while(1)" won't compile in C#. while expects a bool, there is no implicit cast from int to bool.
you're right, you would have to cast it yourself, but it definitely works in C++, but I changed it for a better example
TStamper
What if i was ≥ 2 to start with? A better example would be something like "while(3 < 4)".
ShreevatsaR
@ShreevatsaR- that is why I initialized in comments that i=0, but that example you said would work also, actually I like the way it looks better,easier on the eyes
TStamper
`while( true )`
bobobobo
+10  A: 

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

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

SPWorley
+5  A: 

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

McWafflestix
+13  A: 

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.

Michael Borgwardt
+10  A: 

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

dverespey
+1 for the pun.
Liran Orevi
+3  A: 

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

eKek0
+1  A: 

Yes! .

zvolkov
+8  A: 

Take a look at a for loop.

for ( initialization ; condition ; increment  )

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

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

for(;;)
Dmitri Farkov
+1  A: 

Often used in embedded programming.

-setup interrupts and timers. -then loop forever.

When an interrupt or timer occurs that will be handled.

Maestro1024
+3  A: 

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.

bobobobo
+2  A: 

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

for(;;)

instead of

while(true)

is that

while(true)

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

Stefan