Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does for (;;) mean in Perl?

Tags:

loops

perl

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?

like image 571
Ben Baraga Avatar asked Apr 29 '10 13:04

Ben Baraga


2 Answers

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.

like image 169
Mitch Wheat Avatar answered Nov 06 '22 05:11

Mitch Wheat


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) {
  //...
}
like image 22
polygenelubricants Avatar answered Nov 06 '22 04:11

polygenelubricants