[Tex/LaTex] Problems with formatting LaTeX document pseudo code, ending while loops

algorithmspseudocode

I am writing a LaTeX document for some pseudo code I have written. I have a bunch of while loops, but the way latex is formatting them, it looks like:

while(x)
   while(y)
      while(z)

whereas I want it to do:

while(x)
   while(y)
while(z)

so I tried using \endwhile, and got the following error:

Undefined control sequence.
l.44    \ENDWHILE

I am using

\usepackage{algorithm}
\usepackage{program}

after using wikibooks – Algorithms & Pseudocode site, in which they use \endwhile, so I am wondering what I am doing wrong.

Best Answer

Uhm, you actually only need one of those packages. Pick either one, and it should work.

Example for algorithmic:

\documentclass{article}

\usepackage{algorithmic}

\begin{document}
  \begin{algorithmic}
  \WHILE{condition}
    \WHILE{condition}
      \STATE\COMMENT{do something}
    \ENDWHILE
  \ENDWHILE
  \WHILE{condition}
    \STATE\COMMENT{do something else}
  \ENDWHILE
  \end{algorithmic}
\end{document}

Example for program:

\documentclass{article}

\usepackage{program}

\begin{document}
  \begin{program} 
  \WHILE condition \DO 
    \WHILE condition \DO 
      \COMMENT{do something}
    \OD
  \OD
  \WHILE condition \DO 
    \COMMENT{do something else}
  \OD
  \end{program}
\end{document}