Control Structures: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Shell for loop (Iterative))
(Beefed up a bit...encyclopedia articles should have more information)
 
(165 intermediate revisions by 43 users not shown)
Line 1: Line 1:
[[Category:Encyclopedia]]'''Control structures''' are features of programming languages that influence the code flow. Examples of control structures are:
{{task}}
*loops, which repeat sections of code
*conditions, which selectively execute sections of code
*exceptions, which help in error handling and avoid causing more problems when there is another problem
*jumps (a.k.a. "branches" or "gotos"), which explicitly and usually unconditionally set the next instruction to be executed by the program


Examples of these structures can be found on the following pages:
Control structures are features of programming languages that influence the code flow. Two common examples are loops and conditions. The task here is to demonstrate control structures supported by different programming languages.


=Conditional=
* [[Conditional Structures]]
* [[Flow Control Structures]]

* [[Loop Structures]]
These control structures depend on conditions to control their behavior.
* [[Exceptions]]

* [[Help:Orphaned code]]
==[[C]]==

===if-then-else===
'''Compiler:''' [[GCC]] 4.1.2
int main (void) {
int input = 2;
if ( 3 == input ) {
// Do something
}



if ( 3 == input ) {
// Do something
} else {
// Do something else
}
}

===switch===
'''Compiler:''' [[GCC]] 4.1.2
int main (void) {
int input = 42;

switch (input) {
case 0:
// Do something, because input = 0
break;
case 1:
// Do something, because input = 1
break;
case 2:
// Do something, because input = 2
default:
// Do something else.
break; // Optional
}
}

===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 );
}

==[[C plus plus|C++]]==

===if-then-else===
'''Compiler:''' [[GCC]] 4.1.2
int main (void) {
int input = 2;
if ( 3 == input ) {
// Do something
}



if ( 3 == input ) {
// Do something
} else {
// Do something else
}
}

===switch===
'''Compiler:''' [[GCC]] 4.1.2

int main (void) {
int input = 42;

switch (input) {
case 0:
// Do something, because input = 0
break;
case 1:
// Do something, because input = 1
break;
case 2:
// Do something, because input = 2
default:
// Do something else.
break; // Optional
}
}


===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 );
}

==[[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
}


#!/usr/bin/perl -w
use strict;
my $condition1 = 0;
my $condition2 = 1;
if ( $condition1 ) {
# Do something
} elsif ( $condition2 ) {
# Do somethine else
}


#!/usr/bin/perl -w
use strict;
my $condition1 = 0;
my $condition2 = 1;
if ( $condition1 ) {
# Do something
} else {
# Do something else
}


#!/usr/bin/perl -w
use strict;
my $condition1 = 0;
my $condition2 = 1;
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
}

=Iterative=

These control structure operate on datasets.

==Perl==
===foreach===
'''Interpreter:''' [[Perl]] 5.8.8

#!/usr/bin/perl -w

use strict;

my @array = ("Value1", "Value2", "Value3", "Value4", "Value5");

foreach ( @array ) {
my $item = $_
# Do something with $_.
}

==[[UNIX Shell]]==
===for===
'''Interpreter:''' [[Bourn 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

Latest revision as of 20:37, 22 October 2008

Control structures are features of programming languages that influence the code flow. Examples of control structures are:

  • loops, which repeat sections of code
  • conditions, which selectively execute sections of code
  • exceptions, which help in error handling and avoid causing more problems when there is another problem
  • jumps (a.k.a. "branches" or "gotos"), which explicitly and usually unconditionally set the next instruction to be executed by the program

Examples of these structures can be found on the following pages: