The FORALL loop

The FORALL loop is not part of Algol 68, but an extension introduced by the a68toc compiler. It is similar to the FOR loop, but the identifier has the mode of an element of the multiple under consideration. Look at this example:

   []REAL r1 = (1.0,2.0,3.0,4.0,5.0);
   FORALL e IN r1 DO print(e * e) OD

In the FORALL loop, e takes the value of each element in r1 and so has mode REAL. The compiler generates more efficient code using the FORALL loop by avoiding the normal overheads of the subscripting mechanism. However, the FORALL loop can only be used when all the elements of a dimension are required. If you want to limit the processing to a few elements, you can trim the multiple or use the FOR loop.

The elements of more than one multiple can be combined simultaneously. For example:

   []INT i = (1,2,3,4,5),
         j = (11,12,13,14,15);
   FORALL ii IN i, jj IN j
   DO
      print((ii * jj,newline))
   OD

The comma between ii IN i and jj IN j means that the constructs are elaborated collaterally. The bounds of i must be the same as the bounds of j.

FORALL clauses can be nested as in the case of FOR clauses. If we use l and m declared in a previous example, then

   FORALL ll IN l
   DO
      FORALL mm IN m
      DO
         print(ll * mm)
      OD
   OD

could be used to print the products of all the integers.

Sian Mountbatten 2012-01-19