Control Structures: Difference between revisions
Content added Content deleted
(→[[Tcl]]: Moved to other articles.) |
(→[[UNIX Shell]]: Moved to other articles.) |
||
Line 51: | Line 51: | ||
return 0; |
return 0; |
||
} |
} |
||
==[[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 |
Revision as of 17:02, 25 January 2007
Control Structures
You are encouraged to solve this task according to the task description, using any language you may know.
You are encouraged to solve this task according to the task description, using any language you may know.
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.
This page is being split into multiple articles. Please help move code into these articles, instead:
Iterative
These control structure operate on datasets.
C++
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; }