Tx · LaTeX & TikZ

How to Write Nested for Loops with algorithmicx

Published By the 9to5Science editorial team

At a glance

  1. 1. Load the packages

    Use algorithm for the floating environment and algpseudocode for the pseudocode commands.

  2. 2. Open each loop

    Place the inner For block inside the outer For block.

  3. 3. Close in reverse order

    End the inner loop first, then end the outer loop.

  4. 4. Compile and check

    Confirm each For has a matching EndFor and each statement begins with State.

To typeset a nested for loop with algorithmicx, use the algpseudocode layout and put the inner \For block inside the outer one. Close the blocks in reverse order: first the inner loop, then the outer loop. The layout automatically indents the inner block.

Here is a complete minimal example you can compile with LaTeX.

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}

\begin{algorithm}
\caption{Fill a two-dimensional array}
\begin{algorithmic}[1]
\For{$i \gets 1$ to $n$}
    \For{$j \gets 1$ to $m$}
        \State $A_{ij} \gets 0$
    \EndFor
\EndFor
\end{algorithmic}
\end{algorithm}

\end{document}

The algorithm package supplies the floating algorithm environment and its caption. The algpseudocode package supplies commands such as \For, \State, and \EndFor; it is a layout provided by algorithmicx. The optional [1] on algorithmic turns on line numbering.

How the nesting works

Read the loop structure by matching each opener to its closer:

  1. The first \For starts the outer loop, indexed by ii.
  2. The second \For starts the inner loop, indexed by jj.
  3. \State typesets an instruction within both loops.
  4. The first \EndFor closes the inner loop.
  5. The second \EndFor closes the outer loop.

This is the same nesting rule used in programming languages with block structure: the most recently opened block is closed first. The indentation shown in the output reflects that structure; you do not add spaces to the LaTeX source to control it.

In this example, the intended pseudocode visits every pair (i,j)(i,j) with 1≤i≤n1 \leq i \leq n and 1≤j≤m1 \leq j \leq m, assigning zero to the corresponding array entry. The word to is written as ordinary text in the loop argument as a range convention; algpseudocode formats the pseudocode but does not define how an algorithm executes. If your algorithm uses different bounds or a different stopping condition, put that condition in the loop header instead.

A loop with more than one statement

Use a separate \State for each ordinary instruction. For example, to initialize and then update each entry, write:

\For{$i \gets 1$ to $n$}
    \For{$j \gets 1$ to $m$}
        \State $A_{ij} \gets 0$
        \State $A_{ij} \gets A_{ij} + 1$
    \EndFor
\EndFor

These lines belong to the inner loop because they appear between its \For and \EndFor. If you put a statement after the inner \EndFor but before the outer \EndFor, it belongs to the outer loop but not the inner one.

Common mistakes

  • Leaving out an \EndFor: Every \For needs a matching \EndFor. For nested loops, close them from the inside out.
  • Using \State as a loop command: \State starts an ordinary pseudocode statement; it does not open a loop. Begin the loop with \For.
  • Loading the wrong package or layout: For this syntax, load algpseudocode. Do not load the older algorithmic package alongside it; the packages define overlapping environments and commands.
  • Expecting LaTeX to check the algorithm: A document can typeset a loop structure, but the pseudocode commands do not validate whether the bounds or algorithm are logically correct.
  • Confusing the float with the pseudocode environment: algorithm provides a captionable float, while algorithmic contains the pseudocode. If you do not need a float or caption, you can use algorithmic on its own, but still load algpseudocode for the commands in this example.

For loops with a condition rather than a range, use the corresponding control structure, such as \While{condition} and \EndWhile. The same nesting principle applies: put the inner block inside the outer block and close each block in reverse order.

References

  1. CTAN: algorithmicx package
  2. CTAN: Algorithms bundle

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.