For Loop - 1968: Algol 68

1968: Algol 68

Algol68 has what was considered the universal loop, the full syntax is:

FOR i FROM 1 BY 2 TO 3 WHILE i≠4 DO ~ OD

Further, the single iteration range could be replaced by a list of such ranges. There are several unusual aspects of the construct

  • only the "do ~ od" portion was compulsory, in which case the loop will iterate indefinitely.
  • thus the clause "to 100 do ~ od", will iterate exactly 100 times.
  • the "while" syntactic element allowed a programmer to break from a "for" loop early, as in:
INT sum sq:=0; FOR i WHILE print(("So far:",i, new line)); /*Interposed for tracing purposes.*/ sum sq ≠ 70↑2 /*This is the test for the WHILE*/ DO sum sq +:= i↑2 OD

Subsequent extensions to the standard Algol68 allowed the "to" syntactic element to be replaced with "upto" and "downto" to achieve a small optimization. The same compilers also incorporated:

  • until - for late loop termination.
  • foreach - for working on arrays in parallel.

Read more about this topic:  For Loop