In computer science, a for-loop or for loop is a control flow statement for specifying iteration.
Specifically, a for-loop functions by running a section of code repeatedly until a certain condition has been satisfied.
For-loops can be thought of as shorthands for while-loops which increment and test a loop variable.
It is the direct translation of the earlier German für and was used in Superplan (1949–1951) by Heinz Rutishauser.
This is more explicit in ALGOL versions of the for statement where a list of possible values and increments can be specified.
Generally, for-loops fall into one of four categories: The for-loop of languages like ALGOL, Simula, BASIC, Pascal, Modula, Oberon, Ada, MATLAB, OCaml, F#, and so on, requires a control variable with start- and end-values, which looks something like this: Depending on the language, an explicit assignment sign may be used in place of the equal sign (and some languages require the word int even in the numerical case).
An optional step-value (an increment or decrement ≠ 1) may also be included, although the exact syntaxes used for this differ a bit more between the languages.
It requires 3 parts: the initialization (loop variant), the condition, and the advancement to the next iteration.
This type of "semicolon loops" came from B programming language and it was originally invented by Stephen Johnson.
The advancement to the next iteration part is performed exactly once every time the loop ends.
It is usually characterized by the use of an implicit or explicit iterator, in which the loop variable takes on each of the values in a sequence or other data collection.
Some languages (such as PL/I, Fortran 95) also offer array assignment statements, that enable many for-loops to be omitted.
It is so named because most uses of this construct result in the variable taking on a range of integer values in some orderly sequences (for example., starting at 0 and ending at 10 in increments of 1) Loop counters change with each iteration of a loop, providing a unique value for each iteration.
This style is generally agreed to have originated from the early programming of Fortran[citation needed], where these variable names beginning with these letters were implicitly declared as having an integer type, and so were obvious choices for loop counters that were only temporarily required.
The practice dates back further to mathematical notation where indices for sums and multiplications are often i, j, etc.
This C-style for-loop is commonly the source of an infinite loop since the fundamental steps of iteration are completely in the control of the programmer.
Some languages may also provide other supporting statements, which when present can alter how the for-loop iteration proceeds.
Fortran 90, for example: Thus, when "trouble" is detected in the inner loop, the CYCLE X1 (not X2) means that the skip will be to the next iteration for I, not J.
Situations, where the address of the loop variable is passed as an argument to a subroutine, make it very difficult to check because the routine's behavior is in general unknowable to the compiler unless the language supports procedure signatures and argument intents.
Again, modifications of I would not affect the control of the loop, but now a disjunction is possible: within the loop, references to the value of I might be to the (possibly altered) current value of I or to the auxiliary variable (held safe from improper modification) and confusing results are guaranteed.
For instance, within the loop a reference to element I of an array would likely employ the auxiliary variable (especially if it were held in a machine register), but if I is a parameter to some routine (for instance, a print-statement to reveal its value), it would likely be a reference to the proper variable I instead.
A compiler may prevent such attempts, they may have no effect, or they might even work properly - though many would declare that to do so would be wrong.
The syntax for a three-expression for-loop is nearly identical in all languages that have it, after accounting for different styles of block termination and so on.
ALGOL 58 introduced the for statement, using the form as Superplan: For example to print 0 to 10 incremented by 1: COBOL was formalized in late 1959 and has had many elaborations.
Originally all loops had to be out-of-line with the iterated code occupying a separate paragraph.
Ignoring the need for declaring and initializing variables, the COBOL equivalent of a for-loop would be.
There are several unusual aspects of the construct Subsequent extensions to the standard ALGOL 68 allowed the to syntactic element to be replaced with upto and downto to achieve a small optimization.
The same compilers also incorporated: Decrementing (counting backwards) is using downto keyword instead of to, as in: The numeric range for-loop varies somewhat more.
If the above loops contained only comments, execution would result in the message "syntax error near unexpected token 'done'".
Unlike other languages, AppleScript currently has no command to continue to the next iteration of a loop.
for (i = 0; i < 5; i++)
printf("*");