A simple guide to writing for loops and nested loops in LaTeX

In this guide, you’ll learn how to write for loop in LaTeX using the three most popular packages.

Essential Packages for Writing Algorithms

When writing a research paper, thesis, or report, you often need to present algorithms. LaTeX provides several packages to write pseudocode in a clean, professional way. Among them, the three most widely used are:

  • algorithmic package
  • algorithm2e package
  • algpseudocode package (part of algorithmicx)

In this tutorial, you’ll see for loop and nested loop examples with each package.

The algorithmic package

In this package, you can write a loop using the commands \FOR ... \ENDFOR.

\FOR{$i = 1$ to $10$}
  \STATE Print $i$
\ENDFOR

\FOR{...} → Inside the braces, you specify the loop condition. For instance, i=1 to 10 means that i will run from 1 to 10.

\STATE → Represents an action or a single step in the algorithm. Here, it tells us to Print i.

\ENDFOR → Marks the end of the loop. It works the same way as curly braces { } in programming.

\documentclass{article}
\usepackage{algorithm}
\usepackage{algorithmic}

\begin{document}

\begin{algorithm}
\caption{Simple for loop Example}
\begin{algorithmic}
\FOR{$i = 1$ to $10$}
  \STATE Print $i$
\ENDFOR
\end{algorithmic}
\end{algorithm}

\end{document}

Loop with algorithmic package

The algorithm2e package

This one is very popular because its syntax looks almost exactly like real programming.

\For{$i \gets 1$ \KwTo $10$}{
  Print $i$\;
}

\For{...}{...} → The first {...} defines the loop condition (what the loop runs over), and the second {...} contains the actual body of the loop.

$i \gets 1$ → This means i starts from 1. (Here, \gets is a neat symbol for assignment.)

\KwTo → This means “to,” indicating the range. So the loop continues up to 10.

\; → This is used at the end of each line, just like a semicolon in programming.

{ ... } → Whatever is inside the braces is the loop body.

\documentclass{article}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}

\begin{document}

\begin{algorithm}
\caption{Example with Algorithm2e}
\For{$i \gets 1$ \KwTo $10$}{
  Print $i$\;
}
\end{algorithm}

\end{document}

The algpseudocode package

This package is actually a part of algorithmicx. The code written with it looks much more readable and easy to follow.

\For{$i = 1$ to $10$}
  \State Print $i$
\EndFor

\For{...} → This starts the for loop. Here, the condition is written as $i = 1$ to $10$

\State → This is used to describe what action will be performed inside the loop.

\EndFor → This marks the end of the loop.

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

\begin{document}

\begin{algorithm}
\caption{Example with Algpseudocode}
\begin{algorithmic}
\For{$i = 1$ to $10$}
  \State Print $i$
\EndFor
\end{algorithmic}
\end{algorithm}

\end{document}

Loop with Algpseudocode package

Nested for Loop Example in All Three Packages

A nested loop means running one loop inside another. I’m showing you the syntax of nested loops using three different LaTeX algorithm packages.

% Algorithmic
\FOR{$i = 1$ to $3$}
  \FOR{$j = 1$ to $2$}
    \STATE Print $i, j$
  \ENDFOR
\ENDFOR

% Algorithm2e
\For{$i \gets 1$ \KwTo $3$}{
  \For{$j \gets 1$ \KwTo $2$}{
    Print $i, j$\;
  }
}

% Algpseudocode
\For{$i = 1$ to $3$}
  \For{$j = 1$ to $2$}
    \State Print $i, j$
  \EndFor
\EndFor

Final Note

If you want your research paper/thesis to look professional, pick algorithm2e or algpseudocode. Always keep formatting consistent and avoid mixing packages.

Share tutorial

Leave a Comment

Your email address will not be published. Required fields are marked *