How to Write Nested for Loops with algorithmicx
At a glance
- 1. Load the packages
Use algorithm for the floating environment and algpseudocode for the pseudocode commands.
- 2. Open each loop
Place the inner For block inside the outer For block.
- 3. Close in reverse order
End the inner loop first, then end the outer loop.
- 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:
- The first
\Forstarts the outer loop, indexed by . - The second
\Forstarts the inner loop, indexed by . \Statetypesets an instruction within both loops.- The first
\EndForcloses the inner loop. - The second
\EndForcloses 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 with and , 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\Forneeds a matching\EndFor. For nested loops, close them from the inside out. - Using
\Stateas a loop command:\Statestarts 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 olderalgorithmicpackage 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:
algorithmprovides a captionable float, whilealgorithmiccontains the pseudocode. If you do not need a float or caption, you can usealgorithmicon its own, but still loadalgpseudocodefor 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
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.