Loop structures: Difference between revisions
m Adding syntax highlighting to PHP snippets |
→[[C]]: Removed syntax highlighting |
||
Line 77: | Line 77: | ||
'''Compiler:''' [[GCC]] 4.1.2 |
'''Compiler:''' [[GCC]] 4.1.2 |
||
<highlightSyntax language=C> |
|||
int main (int argc, char ** argv) { |
int main (int argc, char ** argv) { |
||
int condition = 1; |
int condition = 1; |
||
Line 87: | Line 86: | ||
} |
} |
||
} |
} |
||
</highlightSyntax> |
|||
===do-while=== |
===do-while=== |
||
<highlightSyntax language=C> |
|||
int main (int argc, char ** argv) { |
int main (int argc, char ** argv) { |
||
int condition = ...; |
int condition = ...; |
||
Line 104: | Line 101: | ||
} while ( condition ); |
} while ( condition ); |
||
} |
} |
||
</highlightSyntax> |
|||
===for=== |
===for=== |
||
<highlightSyntax language=C> |
|||
int main (int argc, char ** argv) { |
int main (int argc, char ** argv) { |
||
int i; |
int i; |
||
Line 122: | Line 117: | ||
} |
} |
||
} |
} |
||
</highlightSyntax> |
|||
===while with continue=== |
===while with continue=== |
||
Line 130: | Line 124: | ||
You can also do this with do-while and for. |
You can also do this with do-while and for. |
||
<highlightSyntax language=C> |
|||
int main (int argc, char ** argv) { |
int main (int argc, char ** argv) { |
||
int condition = 1; |
int condition = 1; |
||
Line 144: | Line 137: | ||
} |
} |
||
} |
} |
||
</highlightSyntax> |
|||
===while with break=== |
===while with break=== |
||
Line 151: | Line 143: | ||
You can also do this with do-while and for. |
You can also do this with do-while and for. |
||
<highlightSyntax language=C> |
|||
int main (int argc, char ** argv) { |
int main (int argc, char ** argv) { |
||
int condition = 1; |
int condition = 1; |
||
Line 165: | Line 156: | ||
} |
} |
||
} |
} |
||
</highlightSyntax> |
|||
==[[C plus plus|C++]]== |
==[[C plus plus|C++]]== |
Revision as of 14:48, 22 February 2007
These are examples of control structures. You may also be interested in:
You are encouraged to solve this task according to the task description, using any language you may know.
In this task, we document loop structures offered by different languages.
Ada
Simple Loop
The simple loop in Ada produces an infinite loop.
loop -- do something end loop;
Any loop can be terminated by the exit reserved word.
loop -- do something if some-condition then exit; end if; -- do somthing else end loop;
The short-hand notation for expressing the exit condition is:
loop -- do something exit when some-conditon; -- do something else end loop;
While loop
while some-condition loop -- do something end loop;
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:
for I in 1..10 loop Put(I); end loop;
You can also employ a range defined earlier in your code.
subtype Month_Number is Integer range 1..12; for I in Month_Number loop Put(I); end loop;
Ranges are also defined for the indices for arrays. Ada array indices can begin at any value, not just 0 or 1.
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;
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
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. } }
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++
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 ); }
Forth
DO-LOOP
( limit start ) DO ( iterated statements ) LOOP ( limit start ) DO ( iterated statements ) ( increment ) +LOOP LEAVE \ exits a DO loop
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-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 ;
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(true) { foo(); }
do-while
do { foo(); } while (true)
for
for(int i = 0; i < 5; i++) { foo(); }
foreach
Platform: J2SE 1.5.0
Object[] objects; // ... for (Object current : objects[]) { // ... }
int[] numbers; // ... for (int i : numbers) { // ... }
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) alert( element );
newLISP
dotimes
Interpreter: newLISP v.9.0
(dotimes (x 10) (println (+ x 1)))
do-until
Interpreter: newLISP v.9.0
(set 'x 1) (do-until (= x 10) (inc 'x) (println x))
do-while
Interpreter: newLISP v.9.0
(set 'x 1) (do-while (< x 10) (inc 'x) (println x))
for
Interpreter: newLISP v.9.0
(for (x 1 10) (println x))
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 );
for
Interpreter: Perl 5.8.8
#!/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
Interpreter: Perl 5.8.8
#!/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
Interpreter: Perl 5.8.8
#!/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
Interpreter: Perl 5.8.8
#!/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
<highlightSyntax>
while(ok()) { foo(); bar(); baz(); }
</highlightSyntax>
do-while
<highlightSyntax>
$i = 0; do { echo $i; } while ($i > 0);
</highlightSyntax>
for
<highlightSyntax>
for($i = 0; $i < 10; ++$i) { echo $i; }
</highlightSyntax>
foreach
<highlightSyntax>
foreach(range(0, 9) as $i) { echo $i; }
</highlightSyntax> foreach is only used for arrays, which is not obvious from the above example <highlightSyntax>
foreach($array as $key => $value) { echo $key.' is '.$value; }
</highlightSyntax>
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
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()
for x in ["foo", "bar", "baz"]: print x
Does range(10) return an array? The above two examples may be redundant.
Ruby
while
while true do foo end
for
for i in [0..4] do 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
Smalltalk
whileTrue/whileFalse
x := 0. [ x < 100 ] whileTrue: [ x := x + 10.].
[ x = 0 ] whileFalse: [ x := x - 20.].
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] }
UNIX Shell
for
Interpreter: Bourne Again SHell
#!/bin/bash ARRAY="VALUE1 VALUE2 VALUE3 VALUE4 VALUE5" for ELEMENT in $ARRAY do echo $ELEMENT # Print $ELEMENT done
Interpreter: Debian Almquist SHell
#!/bin/sh ARRAY="VALUE1 VALUE2 VALUE3 VALUE4 VALUE5" for ELEMENT in $ARRAY do echo $ELEMENT # Print $ELEMENT done
while
Interpreter: Bourne Again SHell
#!/bin/bash while true do ... done