[Tex/LaTex] Single line while loop in algorithm

algorithm2e

I am using Algorithm2E and I find myself in a situation where I want to express while(!condition);, but I cannot do that on a single line, it seems. This is the closest I got:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[linesnumbered, ruled]{algorithm2e}
\begin{document}

\begin{algorithm}[H]
\SetKwFunction{KwCrit}{critcalSection}
\SetKwFunction{Kwtas}{test-and-set}
\While{$\Kwtas(r) = 1$}{ }
\KwCrit()\\
$r \gets 0$\\
\caption{Pseudocode of test-and-set usage}
\end{algorithm}

\end{document}

But this puts a single line containing only end. It expresses what I want, but looks quite dirty. How would I be able to express this on a single line?

Best Answer

For every code construction \<code> that puts its content on multiple lines, there is an accompanying \l<code> that puts the content on a single line. You can also define your own \WHILE (and accompanying \lWHILE) using \SetKwFor{<macro>}{<start cond>}{<end cond>}{<end construct>}

enter image description here

\documentclass{article}

\usepackage{algorithm2e}

\SetKwFor{WHILE}{while (}{)}{}

\begin{document}

\begin{algorithm}[H]
  \SetKwFunction{KwCrit}{criticalSection}
  \SetKwFunction{Kwtas}{test-and-set}
  \lWhile{$\Kwtas{r} = 1$}{}
  \lWHILE{$\Kwtas{r} = 1$}{}
  \KwCrit{}\;
  $r \gets 0$\;
  \caption{An algorithm}
\end{algorithm}

\end{document}