(A hands-on equivalent of the following is found in Learn to Program: Loops.)


A "for loop" is literally an iteration construct that uses the for keyword. Practically every imperative programming language has it; some examples:


  for (i=0; i<n; ++i) { do_it(i); }            /* C */

  for (my $i=0; $i<$n; ++$i) { &do_it($i); }   # Perl

  for i = 0 to n
    DoIt i
  next i                                       ' Visual Basic

  FOR i FROM 2 TO 10 BY 2 WHILE foo(bar)
  DO
    do it (i);
  OD                                           # Algol 68 #
 
  DO DOIT I = 2, 10, 2                         C FORTRAN

The syntactic pattern of the C construct has been followed by many other languages. The inclusion of the FORTRAN example is explained below.

The exact meaning of the construct varies with the language.

In Algol 68, the loop variable i is implicitly declared, meaning that its value cannot be read or set outside the loop (although a different variable, also named i, may exist there).

Of particular interest is the fact that within the loop, i is treated as a constant: its value cannot be modified. The TO, BY and WHILE parts are all optional; their values are evaluated once when the loop is started.

By contrast, in FORTRAN, C, Perl, Visual Basic, and many other languages, the loop variable is the same as any i outside the loop - although the Perl example changes that by using my - and the variable's value can be freely modified within the loop and outside the loop.

However, it depends on the language whether the loop parameters are evaluated only once, when the loop is entered, or each iteration over, in which case they affect the number of iterations.

Another form of for-loop, similar to the Algol-style one, is found in the Bourne shell: but instead of being an incremented number, the iteration variable takes its values from a given list of words, often obtained through filename globbing or some other list generating construct:


  for i in word1 word2 word3 word4
  do
     doit i
  done

Like in Algol and Pascal, a scope is implicitly created for the variable, and the number of iterations is bounded at the time the loop is entered.


Much more can be said about the details of for loops in various languages, but the above should be enough to get the general idea across: a for loop is typically used to program bounded iteration, a loop for which the maximum number of iterations is already known at the time the loop is started. Some languages even restrict it to allow nothing else.

Meanwhile all these languages have while loops in which the iteration condition can depend on anything that happens in the loop body, including the value of yet unknown data.

This distinction between bounded and unbounded iteration has nothing to do with syntax, but is purely semantical. To a programmer, it is much more relevant than any syntactic detail because it represents a difference in expressive power: some programming problems can be solved by using bounded iteration only while others cannot.

Therefore, a programmer discussing "for loops" may well be referring to bounded iterations. It is for this reason that it makes sense to call the FORTRAN construct above a "for" loop.