Loop structures

From Rosetta Code
Revision as of 16:51, 25 January 2007 by MikeMol (talk | contribs) (Added JavaScript)

AppleScript

repeat-until

set i to 5
repeat until i is less than 0
	set i to i - 1
end repeat
repeat
	--endless loop
end repeat

C

while

Compiler: GCC 4.1.2

int main (int argc, char ** argv) {
  int condition = 1;

  while ( condition ) {
    // Do something
    // Don't forget to change the value of condition.
    // If it remains nonzero, we'll have an infinite loop.
  }
}

C++

Run-Time Control Structures

for

Compiler: GCC 3.3.4

#include <iostream>

int main()
{
 int i = 1;

 // Loops forever:
 for(; i == 1;)
  std::cout << "Hello, World!\n";
}

do-while

Compiler: GCC 4.1.2

int main (void) {
  int condition = 1;

  do {
    // Do something
    // Don't forget to change the value of condition.
    // If it remains nonzero, we'll have an infinite loop.
  } while ( condition );
}

Run-Time Control Structures

while

Compiler: GCC 4.1.2

int main (void) {
  int condition = 1;

  while ( condition ) {
    // Do something
    // Don't forget to change the value of condition.
    // If it remains nonzero, we'll have an infinite loop.
  }
}

do-while

Compiler: GCC 4.1.2

int main (void) {
 int condition = 1;

 do {
   // Do something
   // Don't forget to change the value of condition.
   // If it remains nonzero, we'll have an infinite loop.
 } while ( condition );
}

Java

while

   while(true)
   {
       foo();
   }

do-while

   do
   {
       foo();
   }
   while (true)

for

   for(int i = 0; i < 5; i++)
   {
       foo();
   }

JavaScript

while

   while(true)
   {
       foo();
   }

for

   for(var i = 0; i < 5; i++)
   {
       foo();
   }

Pascal

while

Compiler: Turbo Pascal 7.0

 WHILE condition1 DO
   BEGIN
     procedure1;
     procedure2;
   END;

repeat-until

Compiler: Turbo Pascal 7.0

 REPEAT
   procedure1;
   procedure2;
 UNTIL condition1;

for

Compiler: Turbo Pascal 7.0

 FOR counter=1 TO 10 DO
   BEGIN
     procedure1;
     procedure2;
   END;

Perl

while

Interpreter: Perl 5.8.8

#!/usr/bin/perl -w

use strict;

my $condition1 = 0;

while ( $condition1 ) {
 # Do something.
 # Remember to change the value of condition1 at some point.
}

do-while

Interpreter: Perl 5.8.8

#!/usr/bin/perl -w

use strict;

my $condition1 = 0;

do {
 # Do something.
 # Remember to change the value of condition1 at some point.
} while ( $condition1 );

until

Interpreter: Perl 5.8.8

#!/usr/bin/perl -w

use strict;

my $condition1 = 1;

until ( $condition1 ) {
 # Do something.
 # Remember to change the value of condition1 at some point.
}


do-until

Interpreter: Perl 5.8.8

#!/usr/bin/perl -w

use strict;

my $condition1 = 1;

do {
 # Do something.
 # Remember to change the value of condition1 at some point.
} until ( $condition1 );

PHP

while

while(ok()) {
    foo();
    bar();
    baz();
}

for

for($i = 0; $i < 10; ++$i) {
    echo $i;
}

foreach

foreach(range(0, 9) as $i) {
    echo $i;
}

Python

with

Interpreter: Python 2.5

foo could for example open a file or create a lock or a database transaction:

with foo() as bar:
  baz(bar)


while

while ok():
    foo()
    bar()
    baz()
else:
    # break was not called
    quux() 

for

for i in range(10):
    print i
else:
    # break was not called
    foo()

Ruby

while

while true do
  foo
end

for

for i in [0..4] do
  foo
end