# Problems with formatting LaTeX document pseudo code, ending while loops

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.

-

## migrated from stackoverflow.comAug 24 '11 at 23:28

This question came from our site for professional and enthusiast programmers.

Possibly related: Print programs with its proper syntax –  Werner Aug 24 '11 at 23:34

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}

-
Ah, that uses algorithmic, not algorithm... Now it is working. I definately like algorithmic more than program, so thank you. I was going off of that website. –  Jim Aug 24 '11 at 20:47
Oops, my bad. I misread that in your post, sorry. algorithm only adds the float environments, so with algorithm and algorithmic you should be fine: \begin{algorithm}\begin{algorithmic}...\end{algorithmic}\end{algorithm} –  pulsar Aug 24 '11 at 20:58
wicked, thank you. –  Jim Aug 24 '11 at 20:59

In case anyone else runs into this problem, it turns out that you need to have while-do statements

while{x} \DO
a,b,c = z
while{z} \DO
z = 1 \OD
\OD
while{y} \DO
y += 1 \OD


That seemed to fix the indentation problem for me.

-