For loop
Many programming languages define a For loop as a control structure, with which you can execute a group of statements (block ) with a certain number of repetitions.
The definition look like a For loop has (syntax) is different from language to language. The importance of a For loop (semantics), so the way it is executed is different from language to language. The elements that make up a For loop, but are almost always the same.
Numerical loop
The number of repetitions is already fixed upon entering the loop. There is a loop variable which is initially set to the initial value and is then changed in each case by the step value, until the target value is reached. The loop variable, the initial value, the step width and the end value must be numeric. This type of loop is therefore also known under the term counting loop.
In most programming languages are restricted to integers start, end value, and increment. In some languages (such as Pascal ) is the step size to 1 (or -1 with downto instead to) limited.
The basic structure of these for loops is the following (example BASIC):
For counter = start To end Step n ' to ' repetitive ' instructions Next Expressive loop
The expression-oriented loop allows it to work with non-numeric loop variable. So, for example, linked lists can be edited.
In C-like programming a For loop has the form:
For (initialization; test; continued) statement And it will be executed (according to ISO / IEC 9899:1999 ):
Example for use as non-numeric loop:
Struct list { struct list * next; int element; }; for ( p = list; p = NULL;! p = p -> next ) { ... } Example for use as a numerical loop:
For (i = 0; i
Some programming languages (eg Perl, Python, PHP, but also C 11) offer a construct to assign a variable in sequence all elements of a list.
A foreach loop has the form:
Foreach variable (value ) { statements}
example:
Foreach $ name ( " Anna ", " Heinz ", " Sebastian ") {
print (" Hello, $ name \ n. ");
}
$ name is the variable that gets successively assigned the values in the parentheses.
A foreach loop has the form:
Foreach ( array as keys => value ) {
instructions
}
Keys and value is assigned in each loop a key - value pair from the array.
PHP arrays are different to many other languages in that each entry can be a key - value pair, not just a single value.
Unlike Perl the syntax is similar to the mathematical interpretation, so it sounds funny when you reads the code.
This may in particular result in programming beginners or those switching to problems.
In most other programming languages, namely follows the keyword foreach is the name of the variable that assumes successively the different values .
Example:
php
$ names = array (
"Albert" => " Einstein "
" Rasmus " => " Lerdorf "
" Stephen William " => " Hawking "
);
foreach ( $ names as $ first name => $ last_name) {
print (" Hello, $ first name $ lastname \ n");
}? >
C 11
In the new C standard C 11 (also known as C 0 x), there is the range- based for loop ( engl. range -for ).
This allows the programmer to iterate over all the containers of the STL ( everything the methods begin () and end () iterators and thus provides. . ), But also on traditional arrays (C -style arrays):
# Include
The here used auto keyword brings the compiler to automatically use the required type ( would be at two loops int).
Heinz Rutishauser developed from 1949 to 1951 the simple algebraic programming language " super plan".
Rutishauser knew Konrad Zuse's work on programming languages, ie Zuse Plankalkül and chose the name based on Zuse's name " allocation plan " for a single program.
Rutishausers simple language had only one control structure: the for statement or for loop.
For i = 2 ( 1) n 3 =
means, for example, that in an array a at all (ie it is in increments of 1 counted ) elements from index start value 2 starting up to index n is a target value 3 is added.
Perl
PHP
Historical: The for- loop of "super plan"