An example how to produce ”end if”, ”end for”, and ”end while” in the algorithm2e environment

algorithm2e

Could anyone show me by an example how to produce ''end if'', ''end for'', and ''end while'' in the algorithm2e environment? Hello, thank you for your answer, I have tried to upload the algorithm which I want to write In the algorithm2e environment, I have also tried doing some portion of that but could you please help me to make it error-free and completed? Thanks

\documentclass[11pt]{article}
\usepackage[margin=3cm]{geometry}
\usepackage[ruled, noline]{algorithm2e}
\usepackage{algpseudocode}
\begin{document}

Here is my algorithm

\begin{algorithm}
\SetKw{KwBy}{by}
\SetAlgoLined
\DontPrintSemicolon
$k\gets 0$

$M_a\gets 0$

$M_b\gets 0$

\lWhile{something}{

\For{$p \in \{a,b\}$}{

\lIf{something}{

$M_p \gets M_p +1$
 
something

\Else

\If{M_p=M}{

something


\EndIf

\Else{something}
}
}
}
$k\gets k + 1$

\If{$M_a = M$ and $M_b = M$}{
        $M_a \gets 0$
        
        $M_b \gets 0$
        }
        }
        \end{algorithm}

Best Answer

Remove noend from algorithm2e arguments. noend specifically tells the package not to render end-statements.

Here is your MWE:

\documentclass[11pt]{article}

\usepackage[margin=3cm]{geometry}
\usepackage[ruled, noline]{algorithm2e} % <-- HERE
\usepackage{algpseudocode}

\begin{document}

    \begin{algorithm}
        \caption{Your algorithm}
    
        $i\gets 10$\;
        \eIf{$i\geq 5$}
        {
            $i\gets i-1$\;
        }{
            \If{$i\leq 3$}
            {
                $i\gets i+2$\;
            }
        }
    \end{algorithm}
    
\end{document}

enter image description here