asa172, a FORTRAN90 code which generates all multidimensional indices in a given range. simulating the behavior of an arbitrary number of nested loops, by OFlaherty and MacKenzie.

If the number of dimensions was known in advance, such an operation could be carried out by the corresponding set of nested loops. For example, in 3D:

        do i = i1, i2
          do j = j1, j2
            do k = k1, k2
              (I,J,K) is an index in the range.
            end do
          end do
        end do
One feature of this code, though, is that it can generate all such indexes for an arbitrary number of dimensions.

The code is Applied Statistics Algorithm 172.


The computer code and data files described and made available on this web page are distributed under the MIT license


asa172 is available in a C version and a C++ version and a FORTRAN90 version and a MATLAB version.

Related Data and Programs:


SUBSET, a FORTRAN90 code which enumerates combinations, partitions, subsets, index sets, trees, and other combinatorial objects.


Original FORTRAN77 version by M OFlaherty, G MacKenzie; FORTRAN90 version by John Burkardt.


  1. M OFlaherty, G MacKenzie,
    Algorithm AS 172: Direct Simulation of Nested Fortran DO-LOOPS,
    Applied Statistics,
    Volume 31, Number 1, 1982, pages 71-74.

Source Code:

Last revised on 20 May 2020.