tags:

views:

311

answers:

6

Hello! I was looking though a fellow developers code when I saw this ..

for (;;){
   ....
   ....
   ....
}

I have never seen ";;" used in a loop. What does this do exactly?

+21  A: 

It loops forever. ';;' equates to no start value, no stop condition and no increment condition.

It is equivalent to

while (true)
{
   ...
}

There would usually be a conditional break; statement somewhere in the body of the loop unless it is something like a system idle loop or message pump.

Mitch Wheat
Awesome! Thanks Mitch! Now that I view it as "loops forever because of no specified parameters" it makes perfect sense.
Ben Baraga
The note on infinite loops in "Learning Perl" notes that although the for form is the idiomatic one in Perl is the while form (they recommend while(1) ).
HerbN
@Ben: if you liked that, check out the `-->` operator, as in `while ($x --> 0) { ... }`.
Ether
@Ether - It's not nice to play tricks on future programmers with the parser. ;) I had to play with the RHS of the condition for a sec to make sure I understood what that was doing, as I don't think I'd seen that before in perl5. +1 for making me think.
kbenson
+2  A: 

Infinite Loop (until you break out of it).

It's often used in place of:

while(true) { // Do something }
Justin Niessner
Sounds like a bad idea to use in instead of `while(true) { ... }`. It only confuses noobs.
Hamish Grubijan
For those of us who come from C++ using VS, while(true) throws up a warning when compiling but for(;;) does not. Projects that run 0 compiler warnings are forced to use for(;;) and it carries over when you switch languages.
tloach
0 warnings? Damn!
Hamish Grubijan
+10  A: 

All 3 parts are optional. An empty loop initialization and update is a noop. An empty terminating condition is an implicit true. It's essentially the same as

while (true) {
   //...
}

Note that you it doesn't have to be all-or-nothing; you can have some part and not others.

for (init; cond; ) {
  //...
}

for (; cond; update) {
  //...
}

for (init; ; update) {
  //...
}
polygenelubricants
+4  A: 

Infinite loop. Alot of the time it will be used in a thread to do some work.

    boolean exit = false;

    for(;;) {
        if(exit) {
            break;
        }
        // do some work
    }
BrennaSoft
+2  A: 

It's the same as

while(true) {
   ...
}

It loops forever.

You don't need to specify all of the parts of a for loop. For example the following loop (which contains no body, or update token) will perform a linear search of myArray

for($index = -1; $myArray[++$index] != $target;);
Geoff
+4  A: 

Just like in C, the for loop has three sections:

  • a pre-loop section, which executes before the loop starts.
  • a continuing condition section which, while true, will keep the loop going.
  • a post-iteration section which is executed after each iteration of the loop body.

For example:

for (i = 1, acc = 0; i <= 10; i++)
    acc += i;

will add up the numbers from 1 to 10 inclusive (in C and, assuming you use Perl syntax like $i and braces, in Perl as well).

However, nothing requires that the sections actually contain anything and, if the condition is missing, it's assumed to be true.

So the for(;;) loop basically just means: don't do any loop setup, loop forever (breaks notwithstanding) and don't do any iteration-specific processing. In other words, it's an infinite loop.

paxdiablo