# How to write a "repeat n times" loop in algorithmic?

In algorithmic, I would like to write a loop that will look like the following:

   repeat $n$ times:
[command]
end


I found this question, explaining how to do this in algorithms2e, but it does not work with algorithmic.

EDIT: This is what I do now:

\documentclass{article}

\usepackage{algorithm}
\usepackage{algorithmic}

\begin{document}
\begin{algorithmic}[1]
\WHILE{Repeated at most $T$ times}
\STATE Do Stuff
\ENDWHILE
\end{algorithmic}

\end{document}


• Please provide a full MWE (showing an algorithm with a normal loop for example that you want to change into a repeat n loop), expecially for pseudocode questions it is important to be very clear about the setup. Commented Feb 15, 2023 at 11:58
• @Marijn added example Commented Feb 15, 2023 at 12:06
• We need something that can be compiled on texlive.net/run . ;-) Commented Feb 15, 2023 at 12:08
• @ErelSegal-Halevi as projetmbc mentioned, this code is not compilable. Please add the documentclass and packages, begin/end document, and anything else needed to actually compile the code. Then we can be sure that we have the exact same setup as you, which guarantees that a solution also works for you. Moreover, it saves time for people trying to answer if they don't have to add boilerplate code themselves. Commented Feb 15, 2023 at 12:12
• @Marijn edited, thanks Commented Feb 16, 2023 at 9:20

You can define a new block using algpseudocode: \RepeatN:

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

\begin{document}

\algdef{SE}[REPEATN]{RepeatN}{End}[1]{\algorithmicrepeat\ #1 \textbf{times}}{\algorithmicend}

\begin{algorithmic}
\RepeatN{$n$}
\State Do stuff
\End
\end{algorithmic}

\end{document}


For backwards compatibility with algorithmic you can use algcompatible:

\documentclass{article}
\usepackage{algcompatible}
\usepackage{algorithm}

\begin{document}

\algdef{SE}[REPEATN]{REPEATN}{END}[1]{\algorithmicrepeat\ #1 \textbf{times}}{\algorithmicend}

\begin{algorithmic}
\REPEATN{$n$}
\STATE Do stuff
\END
\end{algorithmic}

\end{document}


• This solution uses algpseudocode, but the project I work on uses algorithmic. When I add \usepacakge{algpseudocode} I get \algorithmic already defined. If I remove algorithmic, then some other parts of the code (e.g. \STATE) stop working. Commented Feb 16, 2023 at 9:22
• algpseudocode is a layout for algorithmicx which is a more customizable replacement of algorithmic. The main difference is that only the first letter of the command is capitalized...
– DG'
Commented Feb 16, 2023 at 9:50
• @ErelSegal-Halevi this illustrates the importance of a full compilable MWE, now you got an answer that you can't easily use because you didn't add your exact setup to your question at first. Commented Feb 16, 2023 at 9:58
• @Marijn You are right. I did not know that there are two versions of algorithmic in two different packages. Commented Feb 16, 2023 at 10:02
• I added a version that is backwards compatible to algorithmic
– DG'
Commented Feb 16, 2023 at 10:06

For comparison a version that uses only the internals of the algorithmic package:

\documentclass{article}

\usepackage{algorithm}
\usepackage{algorithmic}

\makeatletter
\newcommand{\REPEATN}[1]{\ALC@it\algorithmicrepeat%
\ #1 \textbf{times}\begin{ALC@rpt}}%
\newcommand{\ENDREPEAT}{\end{ALC@rpt}\ALC@it\algorithmicend}%
\makeatother

\begin{document}
\begin{algorithmic}[1]
\WHILE{Repeated at most $T$ times}
\STATE Do Stuff
\ENDWHILE
\REPEATN{$n$}
\STATE Do Stuff
\ENDREPEAT
\end{algorithmic}

\end{document}


You can see that the solution of DG' with algcompatible is easier to use.