[Tex/LaTex] Trouble using nested for loops in algorithm

algorithmic

I am trying to write an algorithm which contains nested for loops as follows:

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

\title{Algorithm example}

\author{\LaTeX{}}

\date{\today}

\begin{document}
\maketitle

\section{Gibbs sampling Algorithm}

Algorithms can be included using the commands as shown in algorithm \ref{alg:gibbs}.

\begin{algorithm}
\caption{Collapsed Gibbs sampling algorithm for inference}\label{alg:gibbs}
\begin{algorithmic}[1]
\State Choose initial $z$ and $\xi$.
\For{$T$ iterations}
  \For{$D$ Subjects}
    \For{$N_d$ time segments}
      \State do something
    \EndFor
  \EndFor
\EndFor
\end{algorithmic}
\end{algorithm}

\end{document}

This is the result:
enter image description here

… and I get following errors:

Undefined control sequence
Package algorithmicx: Some blocks are not closed!!!

I am using IEEE's online template and also checked this question about nested for loops with algorithm, with no luck.


EDIT 1:

Changed \Endfor to \EndFor. The errors disappeared but, gives me same output.

Best Answer

I guess that you are compiling your document in "batch mode", which does not stop at errors but tries to continue, skipping problems.

In your case, the complete error message says:

! Undefined control sequence.
l.16 \Endfor

So the problem is that the command \Endfor is not recognized, but tex tries to continue nevertheless, and later produces another arror about blocks not being closed.

The problem is solved by writing \EndFor (which is the correct spelling of this command) instead of \Endfor.

If you want to see the "end for" in the output, remove the noend option in the package ;-)