Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

The algorithmic package lets me make a while loop as

\begin{algorithmic}
  \While{$u\neq v$}
    \State Something
  \EndWhile
\end{algorithmic}

which results in a

while(u is not v) do 
  something 
end while 

How can I make a do-while loop which result in

do
  something
while(u is not v)

(Sorry for the lack of output. I don't know how to add that to my question. I hope the question and the intention is clear enough) I use document class memoir and packages algpseudocode and algorithm.

share|improve this question
    
There's a \Repeat <text> \Until{<condition>} construct, if that helps. Same meaning as do...while, just different words. –  John Wickerson May 23 '13 at 9:03
    
@JohnWickerson Well, strictly speaking, those two constructs do not have the same meaning, since, for the two loops to be equivalent, the condition used after "until" would have to be the negated version of that used after "while". –  Jubobs May 23 '13 at 9:35
1  
Whoops! Good spot :) –  John Wickerson May 23 '13 at 10:12

1 Answer 1

up vote 8 down vote accepted

A do-while construct feels alien and antiquated to me; it's not as easy to parse as while-do. However, if you really want to use such a construct, you can always define it yourself; see below.

Note: you tagged your question with algorithmicx, not algorithmic, so I produced an answer using the algorithmicx package.

enter image description here

\documentclass{article}
\usepackage{algorithmicx}
\usepackage{algpseudocode}

\algdef{SE}[DOWHILE]{Do}{doWhile}{\algorithmicdo}[1]{\algorithmicwhile\ #1}%

\begin{document}
\begin{algorithmic}
  \Do
    \State Something
  \doWhile{$u \neq v$} % <--- use \doWhile for the "while" at the end
\end{algorithmic}
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.