19

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
IAmAN00B
  • 1,913
  • 6
  • 27
  • 38
  • 9
    7 answers in the first minute after posting the question! – SPWorley Jun 11 '09 at 17:25
  • 2
    Functionally it's the same. I prefer while(true) because for loops really have that extra functionality for iterating, beyond just testing the conditional. Some people prefer for(;;) because they read it as forever. – justinhj Jun 11 '09 at 17:27
  • 1
    In C# the 4 characters in "(;;)" actually convert to "ever" - thus forever :) – hugoware Jun 11 '09 at 17:29
  • 8
    @justinhj: Languages like C don't have built-in boolean primitives, so some people prefer for(;;) over while(1). – Andrew Coleson Jun 11 '09 at 17:49
  • you'd think at least one of the 14 nearly identical answers would be accepted by now... – matt b Jun 11 '09 at 18:11
  • That's the problem @matt b! I'll have to write a small randomizer to pick the one to accept! LOL! (actually, I've been working, not logged on) – IAmAN00B Jun 11 '09 at 18:54
  • I feel rather uncomfortable using loops like this. Sometimes I use "while (1 == 1)". – Nippysaurus Jun 12 '09 at 04:59
  • 2
    `for(;;)` is also not affected by prankster co-workers who `#define true false` while you're away from your computer. – Michael Myers Jun 12 '09 at 15:06

17 Answers17

53

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)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 1
    I don't get why this got 22 votes, with other similar answers. – Dmitri Farkov Jun 11 '09 at 17:50
  • 4
    It was right, and it was first. It happens. I didn't earn any rep for it- was already capped today. – Joel Coehoorn Jun 11 '09 at 17:54
  • 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 Jun 11 '09 at 17:59
  • 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 Jun 11 '09 at 18:01
  • 11
    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. – Paul Sonier Jun 11 '09 at 18:15
  • 3
    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 Jun 11 '09 at 18:55
  • Joel is correct, I've even seen posts where that happens to him. It's... weird. – dss539 Jun 11 '09 at 19:54
  • 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 Jun 11 '09 at 20:57
  • 1
    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 Jun 11 '09 at 20:59
  • I would think that the equivalent of `while(true)` should be `for(;true;)`, not `for(;;)`. In fact, I find it weird that the C# compiler admits `f(;;)`. Shouldn't it force a boolean expression in the condition part of the `for`? – a06e Jun 10 '13 at 19:58
23

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

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
Coincoin
  • 27,880
  • 7
  • 55
  • 76
18

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

Brandon E Taylor
  • 24,881
  • 6
  • 47
  • 71
14

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
  • 342,105
  • 78
  • 482
  • 720
12

Yes, It is an infinite loop.

ichiban
  • 6,162
  • 3
  • 27
  • 34
11

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

dverespey
  • 400
  • 2
  • 11
11

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
  • 9,133
  • 1
  • 29
  • 45
10

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
  • 11,550
  • 9
  • 43
  • 63
9

Loop forever.

Otávio Décio
  • 73,752
  • 17
  • 161
  • 228
8

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

matt b
  • 138,234
  • 66
  • 282
  • 345
8

Inifinite loop like saying

while (0<1)
TStamper
  • 30,098
  • 10
  • 66
  • 73
6

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.

Paul Sonier
  • 38,903
  • 3
  • 77
  • 117
4

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

eKek0
  • 23,005
  • 25
  • 91
  • 119
4

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
  • 64,917
  • 62
  • 258
  • 363
4

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
  • 43,293
  • 10
  • 75
  • 117
2

Yes! .

Andriy Volkov
  • 18,653
  • 9
  • 68
  • 83
1

Often used in embedded programming.

-setup interrupts and timers. -then loop forever.

When an interrupt or timer occurs that will be handled.

Maestro1024
  • 3,173
  • 8
  • 35
  • 52