 | Control flow: Encyclopedia II - Control flow - Loops
Control flow - Loops
A loop is a sequence of statements which is specified once but which may be carried out several times in succession. The code "inside" the loop (the body of the loop, shown below as xxx) is obeyed a specified number of times, or once for each of a collection of items, or until some condition is met.
In some languages, such as Scheme, loops are often expressed using tail recursion rather than explicit looping constructs.
Control flow - Count-controlled loops
Most programming languages have constructions for repeating a loop a certain number of times. Note that if N is less than 1 in these examples then the body is skipped completely. In most cases counting can go downwards instead of upwards and step sizes other than 1 can be used.
FOR I = 1 TO N for I := 1 to N do begin
xxx xxx
NEXT I end;
DO I = 1,N for ( I=1; I<=N; ++I ) {
xxx xxx
END DO }
See also For loop, Loop counter.
In many programming languages, only integers can be reliably used in a count-controlled loop. Floating-point numbers are represented imprecisely due to hardware constraints, so a loop such as
for X := 0.1 step 0.1 to 1.0 do
might be repeated 9 or 10 times, depending on rounding errors and/or the hardware and/or the compiler version.
Control flow - Condition-controlled loops
Again, most programming languages have constructions for repeating a loop until some condition changes. Note that some variations place the test at the start of the loop, while others have the test at the end of the loop. In the former case the body may be skipped completely, while in the latter case the body is always obeyed at least once.
DO WHILE (test) repeat
xxx xxx
END DO until test;
while (test) { do
xxx xxx
} while (test);
See also While loop.
Control flow - Collection-controlled loops
A few programming languages (e.g. Smalltalk, Perl, C#) have special constructs which allow you to implicitly loop through all elements of an array, or all members of a set or collection.
someCollection do: [ :eachElement | xxx ].
foreach someArray { xxx }
foreach (string s in myStringCollection) { xxx }
Control flow - General iteration
General iteration constructs such as C's for statement and Common Lisp's do form can be used to express any of the above sorts of loops, as well as others -- such as looping over a number of collections in parallel. Where a more specific looping construct can be used, it is usually preferred over the general iteration construct, since it often makes the purpose of the expression more clear.
Control flow - Infinite loops
Sometimes it is desirable for a program to loop forever, or until an exceptional condition such as an error arises. For instance, an event-driven program may be intended to loop forever handling events as they occur, only stopping when the process is killed by the operator.
More often, an infinite loop is due to a programming error in a condition-controlled loop, wherein the loop condition is never changed within the loop.
Control flow - Early exit from loops
When using a count-controlled loop to search through a table, you may wish to stop searching as soon as you have found the required item. Some programming languages provide a statement such as break or exit, whose effect is to terminate the current loop immediately and transfer control to the statement immediately following that loop. Things can get a bit messy if you are searching a multi-dimensional table using nested loops (see Missing Control Structures below).
Other related archivesAda, Algol 60, Algol 68, AppleScript, BASIC, C, C programming language, C#, C++, COBOL, Common Lisp, Compilers, D, Dahl, Datamation, Donald Knuth, Duff's device, Edsger Dijkstra, Exceptions, For loop, Fortran, Fortran 77, Functional programming, Goto, IBM 704, INTERCAL programming language, Infinite loops, Java, Loop counter, Main loop, Modula-2, Objective C, PHP, PL/1, Pascal, Perl, Python, Quicksort, Recursion, Ruby, Scheme, Self-modifying code, Smalltalk, Spaghetti code, Structured programming, Subroutine, While loop, assembly language, caches, computer programming, computer science, conditions, continuations, goto, identifier, inline, jump table, loop unwinding, machine-, pipelines, program, programming languages, pseudocode, recursion, scripting programming language, spaghetti code, subroutines, tail recursion, trichotomy
 Adapted from the Wikipedia article "Loops", under the G.N U Free Docmentation License. Please also see http://en.wikipedia.org/wiki |