Control Structures: Difference between revisions
Content added Content deleted
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
Control structures are features of programming languages that influence the code flow. Two common examples are loops and conditions. This task here is to demonstrate control structures supported by different programming languages. |
Control structures are features of programming languages that influence the code flow. Two common examples are loops and conditions. This task here is to demonstrate control structures supported by different programming languages. |
||
==Perl== |
==[[Perl]]== |
||
===if-then-else=== |
===if-then-else=== |
||
'''Interpreter:''' Perl 5.8.8 |
'''Interpreter:''' Perl 5.8.8 |
||
#!/usr/bin/perl -w |
|||
use strict; |
use strict; |
||
my $condition1 = 0; |
my $condition1 = 0; |
||
my $condition2 = 1; |
my $condition2 = 1; |
||
if ( $condition1 ) { |
if ( $condition1 ) { |
||
# Do something |
|||
} |
} |
||
if ( $condition1 ) { |
if ( $condition1 ) { |
||
# Do something |
|||
} elsif ( $condition2 ) { |
} elsif ( $condition2 ) { |
||
# Do somethine else |
|||
} |
} |
||
if ( $condition1 ) { |
if ( $condition1 ) { |
||
# Do something |
|||
} else { |
} else { |
||
# Do something else |
|||
} |
} |
||
if ( $condition1 ) { |
if ( $condition1 ) { |
||
# Do something |
|||
} elsif ( $condition2 ) { |
} elsif ( $condition2 ) { |
||
# Do something else |
|||
} else { |
} else { |
||
# Do that other thing |
|||
} |
} |
||
</nowiki></pre> |
|||
===unless=== |
===unless=== |
||
'''Interpreter:''' Perl 5.8.8 |
'''Interpreter:''' Perl 5.8.8 |
||
#!/usr/bin/perl -w |
|||
use strict; |
use strict; |
||
my $condition1 = 0; |
my $condition1 = 0; |
||
unless ( $condition1 ) { |
unless ( $condition1 ) { |
||
# Do something |
# Do something |
||
} |
} |
||
unless ( $condition1 ) { |
unless ( $condition1 ) { |
||
# Do something |
# Do something |
||
} else { |
} else { |
||
# Do something else |
# Do something else |
||
} |
|||
}</nowiki></pre> |
|||
===while=== |
===while=== |
||
'''Interpreter:''' Perl 5.8.8 |
'''Interpreter:''' Perl 5.8.8 |
||
#!/usr/bin/perl -w |
|||
use strict; |
use strict; |
||
my $condition1 = 0; |
my $condition1 = 0; |
||
while ( $condition1 ) { |
while ( $condition1 ) { |
||
# Do something. |
|||
# Remember to change the value of condition1 at some point. |
|||
} |
} |
||
</nowiki></pre> |
|||
===do-while=== |
===do-while=== |
||
'''Interpreter:''' Perl 5.8.8 |
'''Interpreter:''' Perl 5.8.8 |
||
#!/usr/bin/perl -w |
|||
use strict; |
use strict; |
||
my $condition1 = 0; |
my $condition1 = 0; |
||
do { |
do { |
||
# Do something. |
|||
# Remember to change the value of condition1 at some point. |
|||
} while ( $condition1 ); |
} while ( $condition1 ); |
||
</nowiki></pre> |
|||
===until=== |
===until=== |
||
'''Interpreter:''' Perl 5.8.8 |
'''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. |
|||
} |
|||
use strict; |
|||
my $condition1 = 1; |
|||
until ( $condition1 ) { |
|||
# Do something. |
|||
# Remember to change the value of condition1 at some point. |
|||
} |
|||
</nowiki></pre> |
|||
===do-until=== |
===do-until=== |
||
'''Interpreter:''' Perl 5.8.8 |
'''Interpreter:''' Perl 5.8.8 |
||
<pre><nowiki> |
|||
#!/usr/bin/perl -w |
|||
#!/usr/bin/perl -w |
|||
use strict; |
|||
use strict; |
|||
my $condition1 = 1; |
|||
my $condition1 = 1; |
|||
do { |
|||
# Do something. |
|||
do { |
|||
# Remember to change the value of condition1 at some point. |
|||
# Do something. |
|||
} until ( $condition1 ); |
|||
# Remember to change the value of condition1 at some point. |
|||
</nowiki></pre> |
|||
} until ( $condition1 ); |
|||
===for=== |
===for=== |
||
'''Interpreter:''' Perl 5.8.8 |
'''Interpreter:''' Perl 5.8.8 |
||
<pre><nowiki> |
|||
#!/usr/bin/perl -w |
|||
#!/usr/bin/perl -w |
|||
use strict; |
|||
use strict; |
|||
my $limit = 5; |
|||
for ( my $iterator = 0; $iterator < $limit; $iterator++ ) { |
|||
# Do something |
|||
} |
|||
my $limit = 5; |
|||
for ( my $iterator = 0; $iterator < $limit; $iterator++ ) { |
|||
# Do something |
|||
} |
|||
</nowiki></pre> |
|||
===foreach=== |
===foreach=== |
||
'''Interpreter:''' Perl 5.8.8 |
'''Interpreter:''' Perl 5.8.8 |
||
<pre><nowiki> |
|||
#!/usr/bin/perl -w |
|||
#!/usr/bin/perl -w |
|||
use strict; |
|||
use strict; |
|||
my @array = ( 'Value1', 'Value2', 'Value3' ); |
|||
my @array = ( 'Value1', 'Value2', 'Value3' ); |
|||
foreach (@array) { |
|||
my $arrayitem = $_; |
|||
foreach (@array) { |
|||
# Do something |
|||
my $arrayitem = $_; |
|||
} |
|||
# Do something |
|||
</nowiki></pre> |
|||
} |
Revision as of 20:50, 9 January 2007
Control structures are features of programming languages that influence the code flow. Two common examples are loops and conditions. This task here is to demonstrate control structures supported by different programming languages.
Perl
if-then-else
Interpreter: Perl 5.8.8
#!/usr/bin/perl -w use strict; my $condition1 = 0; my $condition2 = 1; if ( $condition1 ) { # Do something } if ( $condition1 ) { # Do something } elsif ( $condition2 ) { # Do somethine else } if ( $condition1 ) { # Do something } else { # Do something else } if ( $condition1 ) { # Do something } elsif ( $condition2 ) { # Do something else } else { # Do that other thing }
unless
Interpreter: Perl 5.8.8
#!/usr/bin/perl -w use strict; my $condition1 = 0; unless ( $condition1 ) { # Do something } unless ( $condition1 ) { # Do something } else { # Do something else }
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 }
foreach
Interpreter: Perl 5.8.8
#!/usr/bin/perl -w use strict; my @array = ( 'Value1', 'Value2', 'Value3' ); foreach (@array) { my $arrayitem = $_; # Do something }