Control Structures: Difference between revisions
Content added Content deleted
(→[[Python]]: Moved to other articles.) |
(Beefed up a bit...encyclopedia articles should have more information) |
||
(48 intermediate revisions by 9 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. |
|||
<font color="#33CC33">This page is being split into multiple articles. Please help move code into these articles, instead: |
|||
* [[Conditional Structures]] |
* [[Conditional Structures]] |
||
* [[Flow Control Structures]] |
* [[Flow Control Structures]] |
||
* [[Loop Structures]] |
* [[Loop Structures]] |
||
* [[Exceptions]] |
* [[Exceptions]] |
||
* [[Help:Orphaned code]] |
|||
=Iterative= |
|||
These control structure operate on datasets. |
|||
==[[C plus plus|C++]]== |
|||
[[Category:C plus plus]] |
|||
===for_each === |
|||
'''Compiler:''' [[GCC]] 4.1.1 |
|||
#include <iostream> // std::cout, std::endl |
|||
#include <vector> // std::vector |
|||
#include <algorithm> // std::for_each |
|||
struct sum |
|||
{ |
|||
int _sum; |
|||
sum() : _sum(0) {}; // Initialize sum with 0; |
|||
void operator() (int a) { _sum += a; } // this function will be called for every element |
|||
}; |
|||
int main() |
|||
{ |
|||
std::vector<int> v; |
|||
v.push_back(10); |
|||
v.push_back(23); |
|||
v.push_back(34); |
|||
/* Note that for_each gets a fresh instance of sum passed, |
|||
* applies every element beginning with *v.begin() up to, |
|||
* but not including v.end() to the function object |
|||
* and returns a copy of it. |
|||
*/ |
|||
sum the_sum = std::for_each(v.begin(), v.end(), sum()); |
|||
std::cout << "The sum is " << the_sum._sum << std::endl; |
|||
return 0; |
|||
} |
|||
==[[Ruby]]== |
|||
[[Category:Ruby]] |
|||
=== 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 |
|||
s |
|||
end |
|||
sum = ['foo', 'bar', 'baz'].inject(0) do |s,x| |
|||
s + x.size |
|||
s |
|||
end |
|||
product = ['foo', 'bar', 'baz'].inject(1) do |p,x| |
|||
p * x.size |
|||
p |
|||
end |
|||
hash = ['foo', 'bar', 'baz'].inject({}) do |h,x| |
|||
h[x] = x.size |
|||
h |
|||
end |
|||
boolean = ['foo', 'bar', 'baz'].inject(true) do |b,x| |
|||
b &&= x != 'bar' |
|||
b |
|||
end |
|||
==[[Tcl]]== |
|||
[[Category:Tcl]] |
|||
=== foreach === |
|||
foreach i {foo bar baz} { |
|||
puts "$i" |
|||
} |
|||
==[[UNIX Shell]]== |
|||
[[Category: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 |
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: