# Loop structures

Loop structures
You are encouraged to solve this task according to the task description, using any language you may know.
Control Structures

These are examples of control structures. You may also be interested in:

In this task, we document loop structures offered by different languages.

Examples to be posted here should instead be posted in an appropriate Iteration page. If a page does not exist demonstrating a particular loop structure, discuss it here.

### Simple Loop

```  -- do something
```

end loop; </Ada> Any loop can be terminated by the exit reserved word. <Ada> loop

```  -- do something
if some-condition then
exit;
end if;
-- do somthing else
```

end loop; </Ada> The short-hand notation for expressing the exit condition is: <Ada> loop

```  -- do something
exit when some-conditon;
-- do something else
```

end loop; </Ada> Any loop can be labeled. The loop name can be specified in the exit statement: <Ada> Outer : loop

```  Inner : loop
-- do something
exit Outer when some-conditon; -- Leaves both loops
-- do something else
end loop Inner;
end loop Outer;
```

</Ada> Note that the name shall reappear in the end loop statement.

### While loop

```   -- do something
```

### For loop

The for loop in Ada iterates over a specified range of discrete values. Ranges are an important concept in Ada. Ranges are always expressed in the form of lowest..highest. The loop control variable always takes on the type of the specified range. The loop control variable is read-only within the loop, and has a scope only within the loop.

Iteration over a literal range: <Ada> for I in 1..10 loop

```  Put(I);
```

end loop; </Ada> You can also employ a range defined earlier in your code. subtype Month_Number is Integer range 1..12; <Ada> for I in Month_Number loop

```  Put(I);
```

end loop; </Ada> Ranges can be combined with a type specification: <Ada> for I in Day_Of_Week range Monday..Friday loop

```  Put(I);
```

end loop; </Ada> Ranges are also defined for the indices for arrays. Ada array indices can begin at any value, not just 0 or 1. <Ada> type Balanced_Index is range -10..10; type My_Array_Type is array(Balanced_Index) of Integer; My_Array : My_Array_Type;

for I in My_Array'Range loop

```  Put(My_Array(I));
```

end loop; </Ada> The keyword reverse is used to step the loop index in the reverse order: <Ada> for I in reverse My_Array'Range loop

```  Put(My_Array(I));
```

## ALGOL 68

### WHILE

```main: (
BOOL condition := TRUE;

WHILE condition DO
# Do something #
# Don't forget to change the value of condition. #
# If it remains nonzero, we'll have an infinite loop. #
OD
)
```

### do-WHILE

```main: (
BOOL condition := ~;

WHILE
# Do something #
# The difference with the first loop is that the #
# code in the loop will be executed at least once, #
# even IF the condition is 0 at the beginning, #
# because it is only checked at the end. #
# Don't forget to change the value of condition. #
# If it remains nonzero, we'll have an infinite loop. #
condition DO SKIP OD
)
```

### FOR

```main: (

FOR i FROM 1 TO 10 BY DO
# The code here will be performed 10 times. #
# The first part in the FOR-statement i FROM 1 is the initialization, #
# and is executed once before the loop begins. #
# The second part is the end condition TO 10, which is checked #
# every time the loop is started, also the first time; #
# the loop ends IF it is false. #
# The third part BY 1 is performed every time the code in the loop #
# is at the end, just before the end condition is checked. #
OD
)
```

A non-obvious feature of the ALGOL 68 FOR loop is that the stripped DO ~ OD clause is valid. Moreover, the initialization and increment parts may be empty. That is, the following is an infinite loop:

```main: (
DO # forever #
SKIP
OD
)
```

A loop like this can still be left with GO TO (see below).

A loop cannot be terminated with an EXIT. The EXIT "completion symbol" merely jumps to the end of the current serial clause (current compound statements) and yields the current value. On the other hand an ALGOL 68 "stop" will terminate the entire program.

This next loop has a loop variable "i" that starts at 1.

```main: (
FOR I DO # forever #
SKIP
OD
)
```

The next loop iterates only a maximum of 100 times:

```main: (
TO 100 DO # 100 times only #
SKIP
OD
)
```

### WHILE with "continue" equivalent

```main:(
BOOL condition := TRUE;

WHILE condition DO
# Do something #

IF other condition THEN
SKIP # do nothing, hence start another iteration #
ELSE
# Do something else #
# This part is not executed IF other condition was true #
FI
OD
)
```

### WHILE with "break" equivalent

The break statement allows you to stop a loop. In ALGOL 68 uses GO TOs, hence can break from any level. You can also do this with do-WHILE and FOR.

```main:(
BOOL condition := TRUE;

WHILE condition DO
# Do something #

IF other condition THEN
GO TO break x # Continue after loop #
FI

# Do something else #
# This part is not executed IF other condition was true #
OD;
break x: SKIP
)
```

A more domestic version of a break allows for only one condition to exit a loop:

```main:(
BOOL break condition := FALSE;
WHILE
# Do something #

NOT break condition DO

# Do something else #
# This part is not executed IF other condition was true #

OD
)
```

### The ALGOL 68 "universal" for/whileloop

``` [ for index ] [ from first ] [ by increment ] [ to last ] [ while condition ] do statements od
The minimum form of a "loop clause" is thus: do statements od
```

This was considered the "universal" loop, the full syntax is:

```for i from 1 by 1 to 3 while i≠4 do ~ od
```

There are several unusual aspects of the construct:

• only the do ~ od portion was compulsory, in which case the loop will iterate indefinitely.
• thus the clause to 100 do ~ od, will iterate only 100 times.
• the while "syntactic element" allowed a programmer to break from a for loop early. eg
```int sum sq:=0;
for i
while
print(("So far:",i,newline));
sum sq≤1000
do
sum sq+:=i↑2
od
```

Subsequent "extensions" to the standard Algol68 allowed the to syntactic element to be replaced with upto and downto to achieve a small optimisation. The same compilers also incorporated:

• until(C) - for late loop termination.
• foreach(S) - for working on arrays in parallel.

The formal specification of ALGOL 68 states:

```for i from u1 by u2 to u3 while condition do action od
```

"is thus equivalent to the following void-closed-clause:"

```begin int f:= u1, int b = u2, t = u3;
step2:
if (b > 0 ∧ f ≤ t) ∨ (b < 0 ∧ f ≥ t) ∨ b = 0
then int i = f;
if condition
then action; f +:= b; go to step2
fi
fi
end
```

## 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
```

### repeat-with

```       repeat with i from 1 to 20
--do something
end repeat
```
```       set array to {1,2,3,4,5}
repeat with i in array
display dialog i
end repeat
```

## BASIC

Works with: QuickBasic version 4.5

### for

By 1's:

```FOR I = 1 TO 10
'...
NEXT I
```

By X's (any number you want):

```FOR I = 1 TO 10 STEP X
'...
NEXT I
```

### while

```DO WHILE(SomeNumber = SomeOtherNumber)
'...
LOOP
```

### do-while

```DO
'...
```

Note: The sign for "not equal to" in BASIC is not !=, but rather <> .

## Befunge

Befunge does not have specific loop statements, but any type of loop construct can be created using the branch (_|) skip (#) and direction (<>v^) commands. For example, the following is one possible structure for a while loop:

```enter > test v
^ body _ exit
```

This program is like a for-loop in other languages, printing the digits from nine down to zero.

```9>:.:#v_@
^ -1 <
```

## Brainf***

BF's only control flow construct of any kind is a loop. Two of the eight commands define the start and end of a conditional loop.

• [ - branch forward past matching ']' if the current cell is zero
• ] - branch back to the matching '[' if the current cell is non-zero

In practice this is equivalent to a "while not zero" loop in other languages.

```[-]  set current cell to zero
```
```[->+>+<<]>>[-<<+>>] copy cell 0 to cell 1, using cell 2 as temporary storage
```

## 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.
}
}
```

### do-while

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

do {
// Do something
// The difference with the first loop is that the
// code in the loop will be executed at least once,
// even if the condition is 0 at the beginning,
// because it is only checked at the end.
// Don't forget to change the value of condition.
// If it remains nonzero, we'll have an infinite loop.
} while ( condition );
}
```

### for

```int main (int argc, char ** argv) {
int i;

for (i=0; i<10; ++i) {
// The code here will be performed 10 times.
// The first part in the for-statement (i=0) is the initialization,
// and is executed once before the loop begins.
// The second part is the end condition (i<10), which is checked
// every time the loop is started, also the first time;
// the loop ends if it is false.
// The third part (++i) is performed every time the code in the loop
// is at the end, just before the end condition is checked.
}
}
```

A non-obvious feature of the C for loop is that an empty condition is equivalent to a true condition. Moreover, the initialization and increment parts may be empty. That is, the following is an infinite loop:

```int main()
{
for (;;) // forever
{
}
}
```

A loop like this can still be left with break (see below) or return (which returns from the function, and therefore also ends the loop).

Another difference compared to while is the behavior of continue. In a for loop, continue will also execute the third clause (the increment part).

### while with continue

The continue statement allows you to continue execution at the beginning of the loop, skipping the rest of the loop. In C you can only do this with the most inner loop. You can also do this with do-while and for.

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

while ( condition ) {
// Do something

if (other condition)
continue; // Continue at the beginning of the loop

// Do something else
// This part is not executed if other condition was true
}
}
```

### while with break

The break statement allows you to stop a loop. In C you can only break from most inner loop. You can also do this with do-while and for.

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

while ( condition ) {
// Do something

if (other condition)
break; // Continue after the the loop

// Do something else
// This part is not executed if other condition was true
}
}
```

## C++

See C

### Compile-Time Control Structures

A compile-time for loop can be generated with template metaprogramming. Example:

```// the loop
template<int start, int finish, template<int n, typename T> class X> struct loop
{
typedef typename X<start, typename loop<start+1, finish, X>::type>::type type;
};

// the termination of the loop
template<int finish, template<int n, typename T> class X> struct loop<finish, finish, X>
{
typedef typename X<finish, void>::type type;
};
```
```// example usage: This implements just a very complicated way of building a multi-dimensional array

// the loop body
template<int n, typename T> struct build_array
{
typedef T type[n];
};

template<int n> struct build_array<n, void>
{
typedef double type;
};

// the loop execution: This is equivalent to
// typedef double array_type[2][3][4][5];
typedef loop<2,6,build_array>::type array_type;
```

## Clojure

### loop

```;; loop/recur is the most general looping construct
(loop [lst [1 3 5 7]
ret []]
(if lst
(recur (rest lst) (conj ret (* (first lst) (first lst))))
ret))
==> [1 9 25 49]
```

## D

### foreach

```// In-place:
auto data = [1, 3, 5, 7];
foreach (ref el; data)
el *= el;
```
```// Creating a new array:
auto data = [1, 3, 5, 7];
typeof(data) data2;
foreach (i, el; data)
data2[i] = el*el;
```

See Pascal

## Factor

### Looping

Most looping is done with recursion. Tail recursion is properly optimized.

```   : forever ( quot -- ) dup slip forever ; inline
[ "A hungry raptor stalks you..." print flush 2000 random sleep ] forever
```

### Iteration

Most indices are implicit or not present at all.

```   3 [ "pint" drink ] times
{ "high" "level" "language" } [ print ] each
high
level
language
10 [ sq ] map
{ 0 1 4 9 16 25 36 49 64 81 }
{ 1 2 3 } { 4 5 6 } [ * ] 2map .
{ 4 10 18 }
10 [ even? ] subset .
V{ 0 2 4 6 8 }
0 10 3 <range> >array .
{ 0 3 6 9 }
10 1 -2 <range> >array .
{ 10 8 6 4 2 }
2222 [ dup 0 > ] [ 2/ dup ] [ ] unfold nip .
{ 1111 555 277 138 69 34 17 8 4 2 1 0 }
```

Iterating with an index:

```   : indexed-alphabet. ( -- )
"abcdefghijklmnopqrstuvwxyz"
[ [ 1string ] [ number>string ] bi* ": " glue print ] each-index ;
```

## Forth

### DO-LOOP

```( limit start ) DO ( iterated statements ) LOOP
( limit start ) DO ( iterated statements ) ( increment ) +LOOP
LEAVE \ exits a DO loop
UNLOOP EXIT \ cleans up loop counters from return stack before returning from the current word
```

example: Two standard iterations

```10 0 DO I . LOOP      \ Prints the numbers from 0 to 9
10 0 DO I . 2 +LOOP   \ Prints the even numbers from 0 to 8
```

### BEGIN-UNTIL

```BEGIN ( iterated statements ) ( conditional ) UNTIL
```

example: Counts down from a given number to zero

```: COUNTDOWN ( n -- )  BEGIN  DUP CR .  1- DUP 0< UNTIL  DROP ;
```

### BEGIN-AGAIN

```BEGIN ( iterated statements ) AGAIN
```

example: echo user's input

```: FOREVER ( -- )   BEGIN  KEY EMIT  AGAIN ;
```

### BEGIN-WHILE-REPEAT

```BEGIN  ( conditional ) WHILE  ( iterated statements )  REPEAT
```

example: counts down from a given number to one

```: COUNTDOWN ( n -- )  BEGIN  DUP WHILE  CR DUP . 1-  REPEAT  DROP ;
```

Additional WHILE clauses may be added to a loop, but each extra WHILE requires a matching THEN after the REPEAT.

### Mixed Structures

Because Forth's compiler is laid bare to the programmer, it is quite easy to both define your own looping structures or combine existing structures in interesting ways. The rules for such combining are somewhat involved, though discussions can be found in the gforth user's manual, among other places. These more complex looping constructs can make up for Forth's lack of a "break" word, and can allow expressing complex loops without resorting to boolean variables. A practical example is also found in the Binary search task.

A good example of a useful combination is this complex loop:

```BEGIN
( condition 1 )
WHILE
( condition 2 )
UNTIL
( condition 2 succeeded )
ELSE
( condition 1 failed )
THEN
```

An example of using this idiom in practice might be this pseudo-Forth

```BEGIN
WHILE
found-record
UNTIL
process-record
ELSE
error" Ran out of records looking for the right one!"
THEN
```

## Groovy

### While Loops

```while (true) {
println 'groovy'
}
```

### For Loops

```
// iterate over a range
x = 0
for (i in 1..3) { x += i }
assert x == 6

// iterate over a list
x = 0
for (i in [1, 2, 3]) { x += i }
assert x == 6

// iterate over an array
x = 0
for (i in (1..3).toArray()) { x += i }
assert x == 6

// iterate over a map's key/value pairs
x = 0
for (i in map) { x += i.value }
assert x = 6

// iterate over a map's values
x = 0
for (i in map.values()) { x += i }
assert x == 6

// iterate over the characters in a string
text = 'abc'
list = []
for (c in text) { list.add(c) }
assert list == ['a', 'b', 'c']
```

### Each

``` def stringList = [ "java", "perl", "python", "ruby" ];

def stringMap = [ "Su" : "Sunday", "Mo" : "Monday", "Tu" : "Tuesday",
"We" : "Wednesday", "Th" : "Thursday", "Fr" : "Friday",
"Sa" : "Saturday" ];

stringList.each() { print " \${it}" }; println "";
// java perl python ruby

stringMap.each() { key, value -> println "\${key} == \${value}" };
// Su == Sunday
// We == Wednesday
// Mo == Monday
// Sa == Saturday
// Th == Thursday
// Tu == Tuesday
// Fr == Friday

stringList.eachWithIndex() { obj, i -> println " \${i}: \${obj}" };
// 0: java
// 1: perl
// 2: python
// 3: ruby

stringMap.eachWithIndex() { obj, i -> println " \${i}: \${obj}" };
// 0: Su=Sunday
// 1: We=Wednesday
// 2: Mo=Monday
// 3: Sa=Saturday
// 4: Th=Thursday
// 5: Tu=Tuesday
// 6: Fr=Friday
```

Most of the usual applications for loops are realized in Haskell by operations on (lazy) lists, like map, fold or filter. It's unusual to use loops in an imperative style. However, if one insists on it, it's easy to make your own implementation of any loop variant.

Here are a few examples:

### Pre-checked loop (while)

```whileM :: Monad m => m Bool -> m a -> m ()
whileM cond body =
cond >>= \b -> if b then body >> untilM cond body else return ()
```

### Post-checked loop (repeat-until)

```untilM :: Monad m => m Bool -> m a -> m ()
untilM cond body =
body >> cond >>= \b -> if b then return () else untilM cond body
```

### For-style loop

Simplest done by iterating over a list:

```forM :: Monad m => [a] -> (a -> m ()) -> m ()
forM []     f = return ()
forM (x:xs) f = f x >> forM xs f
```

## IDL

It should be noted that IDL programmers tend to avoid loops -- most of the time loops are used to access the elements of arrays or vectors, and since IDL is an array language the same purpose can almost always be served in a faster, more elegant and more readable way though any of the array operations.

### for

``` for i=0,50,2 do print,i
```

prints out every second number starting at 0 stopping at 50. Wherever a single command can go in IDL, there can always go a begin...end pair with arbitrary amount of code in between. Thus the above can also read

``` for variable = start, stop [,increment] do begin
;some code here
;some more code
endfor
```

It is allowed but not required to use the appropriate "type of end" - i.e. it would be allowed to just say "end" instead of "endfor". However "endfor" (and "endwhile", "endif" etc) will throw an error if the wrong one is encountered at compile time and thus it is recommended to always use the more descriptive form since it makes debugging a lot easier.

### while

``` while condition do command
```

Same extensions as above:

``` while running do begin
; various snippets of code that might change the variable
; "running" from something true to something false
end[while]
```

### repeat

``` repeat command until condition
```

etc

### goto

'Goto' exists and can in principle be forced to make a loop:

``` label:
;some code
[if condition then \$]
goto label
```

### break

The break statement will immediately terminate the current innermost for, while, repeat, if, case or switch without having to resort to a goto.

## Java

### while

```   while(booleanConditionOrVariable)
{
foo();
}
```

### do-while

```   do
{
foo();
}
while (booleanConditionOrVariable)

```

### for

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

### foreach

Works with: J2SE version 1.5.0+
``` Object[] objects;
// ...
for (Object current : objects[]) {
// ...
}
```
``` int[] numbers;
// ...
for (int i : numbers) {
// ...
}
```
``` Collection<Type> collect;
// ...
for(Type i : collect){
// ...
}
```

### while with continue

```while(booleanConditionOrVariable){
//[code]
if(anotherBooleanConditionOrVariable){
continue;
//"continue" skips this code and goes through the loop again
}
//"continue" skips this code and goes through the loop again
}
```

This also works in a for loop, but the conditional and iteration code (second and third arguments) will be run before the loop begins again.

### while with break

```while(booleanConditionOrVariable){
//[code]
if(anotherBooleanConditionOrVariable){
break;
//"break" skips this code and leaves the loop
}
//"break" skips this code and leaves the loop
}
```

This also works in a for loop, and no code from the arguments of the loop will be run again.

## JavaScript

### while

```   while(true) {
foo();
}
```

### do while

```   do {
foo();
} while(test);

```

### for

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

### for in

```//iterate through property names of an object

var obj = {prop1:"a", prop2:"b", prop3:"c"};
for (var key in obj)
alert( key + ' is set to ' + obj[key] );
```

### for each in

```//iterate through property values of an object

var obj = {prop1:"a", prop2:"b", prop3:"c"};
for each(var element in obj)
```

## Logo

```forever [right random 360 forward 10]
```
```repeat 5 [right 180-36 forward 100]
```

Repeat and forever also have access to a loop counter, starting at 1.

```repeat 10 [print repcount]
```
```while [:x > 0] [make "x :x / 2]
do.while [make "x :x / 2] [:x > 0]
```
```until [:x = 0] [make "x :x / 2]
do.until [make "x :x / 2] [:x = 0]
```

## LSE64

LSE's loop words all work via tail recursion, like Scheme, by re-executing the current word. If used interactively, "repeat" works upon the current line. Exiting words, like "&&", "||", and "exit" can be used to exit an otherwise infinite loop (see other tasks).

```infinite : " again and " ,t repeat
limited : continue? &repeat
```

Counted loops execute a specified word n times. Within that word, "count" accesses a loop counter, counted down to zero.

```body : count , sp
10 body iterate    # 10 9 8 7 6 5 4 3 2 1

body? : count , sp  count 5 >
10 body? &iterate   # 10 9 8 7 6 5
```

## Make

```Make does looping through recursion.
SUCC=`expr \$* + 1`
MAX=10

all: 0-n;

%-n: %-echo
@-make -f loop.mk \$(SUCC)-n MAX=\$(MAX)

%-echo:
@echo \$*

\$(MAX)-n: \$(MAX)-echo;
```

Invoking it

```|make -f loop.mk MAX=2
0
1
2
```

## newLISP

Works with: newLISP version v.9.0

### dotimes

```(dotimes (x 10) (println (+ x 1)))
```

### do-until

```(set 'x 1)
(do-until (= x 10) (inc 'x) (println x))
```

### do-while

```(set 'x 1)
(do-while (< x 10) (inc 'x) (println x))
```

### for

```(for (x 1 10) (println x))
```

See Pascal

See C

## OCaml

### for

<lang ocaml># for i = 0 to 4 do

```   Printf.printf "%d\n" i;
done;;
```

0 1 2 3 4 - : unit = ()</lang>

### for down to

<lang ocaml># for i=4 downto 0 do

```   Printf.printf "%d\n" i;
done;;
```

4 3 2 1 0 - : unit = ()</lang>

### while

<lang ocaml># let line = ref "" in

``` while !line <> "exit" do
line := input_line stdin;
print_endline(String.uppercase !line);
done;;
```

objective caml OBJECTIVE CAML exit EXIT - : unit = ()</lang>

### let rec

The for and while loops are imperative features of OCaml, and most often it is rather recommended to prefer using functional loop designed with recursive functions, or better when iterating over a list or an array using a built-in iterator from the standard library or the extLib library. <lang ocaml>let rec loop i =

``` Printf.printf "%d\n" i;
if i <= 4 then loop (i + 1)
```

in loop 0</lang>

### Built-in Iterators

<lang ocaml>List.iter List.fold_left Array.iter Array.iteri</lang>

## Pascal

### while

Works with: Turbo Pascal version 7.0
``` WHILE condition1 DO
BEGIN
procedure1;
procedure2;
END;
```

### repeat-until

Works with: Turbo Pascal version 7.0
``` REPEAT
procedure1;
procedure2;
UNTIL condition1;
```

### for

Works with: Turbo Pascal version 7.0
``` FOR counter := 1 TO 10 DO
BEGIN
procedure1;
procedure2;
END;
```
Works with: Delphi version 1.x+
``` Var
```
```  Counter : char ;
```
``` FOR counter := 'A' TO 'Z' DO
BEGIN
procedure1;
procedure2;
END;
```

## Perl

Works with: Perl version 5.8.8

### while

```#!/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

```#!/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

```#!/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

```#!/usr/bin/perl -w

use strict;

my \$condition1 = 1;

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

### for

```#!/usr/bin/perl -w

use strict;

my \$limit = 5;

for ( my \$iterator = 0; \$iterator < \$limit; \$iterator++ ) {
# Do something
}

# for-variant, implicit iteration
for (0..\$limit) {
# Do something
}

do_something() for 0..\$limit;
```

### foreach

```#!/usr/bin/perl -w

use strict;

my @numbers = (1, 2, 3);
my %names = (first => "George", last => "Jetson");

foreach my \$number (@numbers) {
# Do something with \$number
}

foreach my \$key (keys %names) {
# Do something with \$key (values are accessible as %names{\$key} )
}
```

### map

```#!/usr/bin/perl -w

use strict;

my @numbers = (1, 2, 3);
my @target;

@target = map {
# Do something with \$_
} @numbers;

@target = map(\$_ + 1, @numbers);

sub a_sub {
# Do something with \$_
}

@target = map a_sub @numbers;
```

### grep

```#!/usr/bin/perl -w

use strict;

my @people = qw/Bobbie Charlie Susan/;
my @target;

@target = grep {
# Discriminate based on \$_
} @people;

# Feed grep into map, this picks out elements 1, 3, 5, etc.
@target = map(\$people[\$_], grep(\$_ & 1, 0..\$#people));

# Pick out the diminutive names
@target = grep(/ie\$/, @people);

sub a_sub {
# Do something with \$_, and return a true or false value
}

@target = grep a_sub @people;
```

## PHP

### while

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

### do-while

```\$i = 0;
do {
echo \$i;
} while (\$i > 0);
```

### for

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

### foreach

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

foreach is only used for arrays, which is not obvious from the above example

```foreach(\$array as \$key => \$value) {
echo \$key.' is '.\$value;
}
```

## Pop11

### while

Pop11 offers multiple looping constructs. Basic one is while loop:

``` 5 -> n;
while n > 0 do
printf(n, '%p\n');
n - 1 -> n;
endwhile;
```

### until

Variant of while is until loop:

```until condition do /* Action */ enduntil;
```

is equivalent to:

```while mot(condition) do /* Action */ endwhile;
```

### for

One can process all elements of a list:

``` for x in [a b c] do x => endfor;
```

It is possible to simultaneously process multiple lists:

```for x y in [a b c], [1 2 3] do [^x ^y] => endfor;
```

in first iteration sets x to "a" and y to 1, in the second x is "b" and y is 2, in the third (last) iteration x is "c" and y is 3. The iteration ends when the shortest list is exhausted.

Sometimes one wants to process tails of the list, to do this use on keyword instead of in keyword:

```for x on [a b c] do x => endfor;
```

in first iteration sets x to [a b c], in the second to [b c], etc...

There is also "counting" version of for loop:

```for x from 2 by 2 to 7 do x => endfor;
```

goes trough 2, 4 and 6. Ommited by frase means by 1.

There is alse a C-like for loop:

```for action1 step  action2 till condition do /* Actions */ endfor;
```

is equivalent to

```action1
while not(condition) do
/* Actions */
action2
endwhile;
```

There are more specialized kinds of loops, but we skip them here.

### quitloop quitif quitunless

Inside loops one can use control transfers to prematurely exit the loop or end current iteration and start the next one:

```while true do n - 1 -> n; quitif(n=0); endwhile;
```

quits loop when n=0. quitloop unconditionally quits loop, quitunless(x) is equivalent to quitif(not(x)).

### nextloop nextif nextunless

Similarely to quitloop nextloop unconditionally ends current iteration and starts the new one, nextif(x) ends current iteration when x is true, nextunless(x) is equivalent to nextif(not(x)). The loop control transfers can be also used inside for (and until) loops.

Finally, it is frequently possible to avoid explicit iteration using higher order map functions (like appdata and mapdata).

## PostScript

The "for" operator expects three numbers and a procedure on the stack. It will consume all four arguments then it will push the first number on the stack, execute the procedure, increase the first number by the second number, push it on the stack, execute the procedure etc until the third number is reached. For example

```10 12 200
{dup moveto 100 0 lineto}
for
stroke
```

will add lines to the currentpath that start at coordinates {10,10}; {22,22}; {34,34} ... and all end at {100,0}. The "stroke" operator then renders these lines on the current output device (usually a screen or a piece of paper).

## Python

### for

Typically for is used to iterate over items in an iterable. Most Python built-in objects support iteration: lists, tuples, strings (including unicode string objects), dictionaries (by keys), sets, ranges (including the results from calls to the xrange() built-in functions) and files (line by line). Any object can support iteration by adding special methods.

<lang python>

```for x in ["foo", "bar", "baz"]:
print x
```

</lang>

Frequently one wants to both iterate over a list and increment a counter:

<lang python>

```mylist = ["foo", "bar", "baz"]
for i, x in enumerate(mylist):
print "Element no.", i, " is", x
```

</lang>

Iterating over more than one list is achieved by zip:

<lang python>

```for x, y, z in zip(lst1, lst2, lst3):
print x, y, z
```

</lang>

Iterating over more than one list + incrementing a counter:

<lang python>

```for counter, [x, y, z] in enumerate(zip(lst1, lst2, lst3)):
print counter, x, y, z
```

</lang>

The built-in range() and xrange() functions are commonly used to provide indexes over which to iterate. range() returns a list while xrange() returns a generator object without building the list in memory:

<lang python>

```import sys
for i in xrange(sys.maxint):
print i,
```

</lang>

This would usually result in a MemoryError if we attempted to use the range() because it would try to create a list in memory consisting of all the integers from 0 up to the system's maximum. However, with xrange() this will run without error (for a very long time or until it's interrupted).

Optional else suite is executed unless break was called during iteration:

<lang python>

```for i in numbers:
if item < 0:
break
else:
print 'all numbers are positive'
```

</lang>

Because of the pervasive support for iteration and generators in Python classes and libraries the for loop structure is far more commonly used than while.

### list comprehension expressions

Typically used when you want to create a list and there is little logic involved. Faster than for loop:

```positives = [n for n in numbers if n > 0]
```

A list comprehension is an expression rather than a statement. This allows them to be used as arguments to a function:

<lang python>

```  def square_each(n):
results = []
for each in n:
results.append(each * each)
return results
squares_3x5 = square_each([x for x in range(100) if (x%3)==0 and (x%5)==0])
# Return a list of all the squares of numbers from 1 up to 100 those numbers are
# multiples of both 3 and 5.
```

</lang>

### while

Typical use:

<lang python>

```while True:
# Do stuff...
if condition:
break
```

</lang>

You can add optional else, which is executed only if the expression tested was false. Typically used for searches.

<lang python>

```while True:
# Do stuff...
if found:
results = ...
break
else:
```

</lang>

Since Python has no "bottom-tested" loop construct (such as "do ... until") ... it's common Python practice to either rethink the design in terms of iteration or to use an while 1 (infinite loop) and break out of it as appropriate.

## Raven

### each

```10 each as i                                     # counted loop
"index is %(i)d\n" print

'hello world' each as c                          # string characters
"%(c)c" print

[ 'a' 'b' 'c' ] each as item                     # list items
"%(item)s" print

{ 'a' 1 'b' 2 } each pair as k, v                # hash key/val pairs
"%(k)s => %(v)d\n" print

'SELECT * FROM table' mysql query each as row    # iterable resource
row print
```

### repeat while/until

```repeat <some_condition> while
<some_process>

repeat <come_condition> until
<some_process>
```

## Ruby

### while

```while true do
foo
end
```

### for

```for i in 0..4 do
foo
end
```

Optionally:

```0.upto(4) do #startNumber.upto(endNumber)
foo
end
```

or:

```5.times do
foo
end
```

Step size other than 1:

```0.upto(10,2) do #startNumber.upto(endNumber, stepSize)
foo
end
```

### each

```['foo', 'bar', 'baz'].each do |x|
puts x
end
```

### collect

```array = ['foo', 'bar', 'baz'].collect do |x|
foo x
end
```

### map

```array = ['foo', 'bar', 'baz'].map {|x| foo x }
```

### inject

```string = ['foo', 'bar', 'baz'].inject("") do |s,x|
s << x
end
```
```sum = ['foo', 'bar', 'baz'].inject(0) do |s,x|
s + x.size
end
```
```product = ['foo', 'bar', 'baz'].inject(1) do |p,x|
p * x.size
end
```
```boolean = ['foo', 'bar', 'baz'].inject(true) do |b,x|
b &&= x != 'bar'
end
```

## Seed7

In Seed7 new statements can be declared easily. This feature is not explained here. Here are examples of the predefined loops:

### while

```while TRUE do
foo;
end while;
```

### repeat

```repeat
foo;
until FALSE;
```

### for

```for i range 0 to 4 do
foo;
end for;
```
```for i range 4 downto 0 do
foo;
end for;
```
```for stri range []("foo", "bar", "baz") do
writeln(stri);
end for;
```

## Smalltalk

### timesRepeat

``` 10 timesRepeat: [ expression ].
```

### iterating over a collection

``` ( collection ) do: [ expression ].
```
``` ( collection ) collect: [:element | element sendMessageToElement].
```
``` #(1 2 3 4) inject: 5 into: [:sum :number | sum + number]
```

### whileTrue/whileFalse

``` x := 0.
[ x < 100 ]
whileTrue: [ x := x + 10.].
```
``` [ x = 0 ]
whileFalse: [ x := x - 20.].
```

### simple looping

``` 1 to: 5 do: [:number | Transcript show: 'Here is a number: ' number printString ; cr]
```
``` (-50 to: 50 by: 4) do: [:number | Transcript show: number ; cr]
```

## SNUSP

```==in==!/==body==?\==out==
\==ydob===/
```

The basic loop structure can be modified in many ways. You can have a pre-loop test by including ? before the loop.

A unique feature of SNUSP is the ability to write bi-directional loops, designed for different effect depending on which direction the flow of control is running around the loop. The two entry points into this subroutine will move a value either up or down one cell, determined by the direction the loop is circulating.

```         #    #
up1====?!/->+<\
?    ?
down1==?!\<+>-/
#    #
```

## Tcl

### foreach

```foreach i {foo bar baz} {
puts "\$i"
}
```

Note that foreach also accepts multiple variables:

```foreach {x y} {1 2 3 4} {
puts "\$x,\$y"
}
```

And also multiple lists:

```foreach i {1 2 3} j {a b c} {
puts "\$i,\$j"
}
```

Or any combination of variables/list:

```foreach i {1 2 3} {x y} {a b c d e f} {
puts "\$i,\$x,\$y"
}
```

### for

``` for {set i 0} {\$i < 10} {incr i} {
puts \$i
}
```

It bears noting that the three parts of the for loop do not have to consist of "initialize variable", "test value of variable" and "increment variable". This is a common way to think of it as it resembles the "for" loop in other languages, but many other things make sense. For example this for-loop will read a file line-by-line:

``` set line ""
for { set io [open test.txt r] } { ![eof \$io] } { gets \$io line } {
if { \$line != "" } { ...do something here... }
}
```

(This is a somewhat awkward example; just to show what is possible)

### while

``` set i 0
while {\$i < 10} {
puts [incr i]
}
```

## Toka

### countedLoop

Counts up or down until the boundaries are met.

```10 0 [ i . ] countedLoop
0 10 [ i . ] countedLoop
```

### whileTrue

Repeatedly executes a quote until the quote returns true.

```100 [ dup . 1 - dup 0 <> ] whileTrue
```

### whileFalse

Repeatedly executes a quote until the quote returns true.

```0 [ dup . 1 + dup 101 = ] whileFalse
```

## UNIX Shell

### for

Interpreter:' Bourne Again SHell

A simple loop that counts from 1 to 10.

```#!/bin/bash
for ((x = 1; \$x <= 10; x = \$x + 1 ))
do
echo \$x
done
```

### for...in

Works with: Bourne Again SHell

A loop that iterates through all of the (whitespace-delimited) members of ARRAY.

```#!/bin/bash
ARRAY="VALUE1 VALUE2 VALUE3 VALUE4 VALUE5"

for ELEMENT in \$ARRAY
do
echo \$ELEMENT # Print \$ELEMENT
done
```

### while

Works with: bourne SHell
```#!/bin/sh
x=1
while [ \$x -lt 101 ]
do
echo \$x
x=`expr "\$x" + 1`
done
```

Works with: Bourne Again SHell
``` #!/bin/bash
while true
do
...
done
```

## V

while loop counting down from 100

```100
[0 >]
[dup puts pred]
while
```

## Visual Basic .NET

Visual Basic has three principal loop structures, Do/Loop, For, and For-Each.

### Do/Loop

```   Sub InfiniteLoop()
Do
'code
Loop
End Sub

Sub LoopWithInnerCondition()
Do
'code
If Condition() Then Exit Do
'more code
Loop
End Sub

Sub PreConditionLoop1()
Do While Condition() 'loop continues while Condition is true
'code
Loop
End Sub

Sub PreConditionLoop2()
Do Until Condition() 'loop continues until Condition is true
'code
Loop
End Sub

Sub PostConditionLoop1()
Do
'code
Loop While Condition() 'loop continues while Condition is true
End Sub

Sub PostConditionLoop2()
Do
'code
Loop Until Condition() 'loop continues until Condition is true
End Sub

Sub ClassicLoop() 'not generally used
While Condition() 'loop continues while Condition is true
'code
End While
End Sub
```

### For

• The type of the iterator variable (i in these example) is infered from the upper and lower bounds.
• If a Step clause isn't provided, it is assumed to be 1.
• Loops can be left early using Exit For
• The upper bound is inclusive.
```   Sub ForLoop1()
For i = 1 To 10
'code
Next
End Sub

Sub ForLoop2() 'skips even numbers
For i = 1 To 10 Step 2
'code
Next
End Sub

Sub ForLoop3() 'iterates backwards
For i = 10 To 1 Step -1
'code
Next
End Sub
```

### For Each

```   Sub ForEachLoop() 'iterates over a collection
For Each item In Source()
'code
Next
End Sub

Function ForEachLoop2() 'searching for a string, exiting early if found
Dim found As Boolean
For Each item In Source()
If item = "CAR" Then
found = True
Exit For
End If
Next
End Function
```

## XSLT

The <xsl:for-each> element allows explicit iteration over a collection of nodes:

```<xsl:for-each select="node set expression">
<!-- executed for each node in the node set. Each node becomes the context for this block. -->
</xsl:for-each>
```

The XPath node set expression for "select" must return a set of nodes:

```PRE                       <!-- all <PRE> blocks in an HTML page -->
group[ignore != 'true']   <!-- all "group" nodes in the current context without an ignore attribute set to 'true' -->
```

Much other iteration in XSLT is implicit. For example, you can specify a "match" attribute to apply a template to all nodes of a particular type.