Loop structures: Difference between revisions
Content added Content deleted
(Added Perl) |
(Added Python) |
||
Line 172: | Line 172: | ||
# Remember to change the value of condition1 at some point. |
# Remember to change the value of condition1 at some point. |
||
} until ( $condition1 ); |
} until ( $condition1 ); |
||
==[[Python]]== |
|||
[[Category: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() |
Revision as of 16:35, 25 January 2007
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 ); }
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 );
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()