[Tex/LaTex] While loop to if condition

algorithm2e

I would like to change my algorithm from while loop to if condition. Every thing will be the same except the while statement. On while loop, change it to r < R. I have tried many ways, but my script missed up.

Here is my original script for the while

\documentclass[conference]{IEEEtran}
  \usepackage{algorithm}
\usepackage[algo2e]{algorithm2e}
\usepackage[]{algpseudocode}

 \begin{algorithm}
    \caption{Pseudo-code of Mine}
    \SetKwInOut{Input}{Input}
    \SetKwInOut{Output}{Output}
    \Input{Info}
    \Output{SCORE}
            \While{$r < R$}{
            \State 
            \begin{varwidth}[t]{\linewidth} \par
                $n\gets function(DataX, Datax1, DataX2,$ \par
                \hskip\algorithmicindent $Type, Type2)$
            \end{varwidth}
            \State $S\gets function (S)$
            \EndWhile}


        \State $SCORE \gets Trained ANN(InfoInput)$ 
\end{algorithm}

enter image description here

My trying code is also to add else condition:

\begin{algorithm}
    \caption{Pseudo-code of Mine}
    \SetKwInOut{Input}{Input}
    \SetKwInOut{Output}{Output}
    \Input{Info}
    \Output{SCORE}
            \if{$r < R$}{
            \State 
            \begin{varwidth}[t]{\linewidth} \par
                $n\gets function(DataX, Datax1, DataX2,$ \par
                \hskip\algorithmicindent $Type, Type2)$
            \end{varwidth}
            \State $S\gets function (S)$
            \Endif}


        \State $SCORE \gets Trained ANN(InfoInput)$ 

        \Else

        {
            \State 
            \begin{varwidth}[t]{\linewidth} \par
                $n\gets function(DataX, Datax1, DataX2,$ \par
                \hskip\algorithmicindent $Type, Type2)$
            \end{varwidth}
            \State $S\gets function (S)$
            \Endif}


        \State $SCORE \gets Trained ANN(InfoInput)$ 

\end{algorithm}

Best Answer

You are using the syntax of two different algorithm packages incorrectly. It seems like you're favouring algorithm2e for the general structure, while using an algorithmicx-type syntax for some of the other components.

Below I've used the traditional algorithm2e syntax to provide you with the layout you're after:

enter image description here

\documentclass{article}

\usepackage[ruled]{algorithm2e}
\usepackage{amsmath}

\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
\DontPrintSemicolon

\begin{document}

\begin{algorithm}
  \caption{Some algorithm caption}

  \Input{Info}
  \Output{SCORE}

  \If{$r < R$}{
    $n \gets \text{function}(\text{DataX}, \text{DataX1}, \text{DataX2}, \text{Type}, \text{Type2})$\;
    $S \gets \text{function}(S)$\;
  }
  $\text{SCORE} \gets \text{Trained ANN}(\text{InfoInput})$\;
\end{algorithm}

\end{document}

Specifically note:

  • Use a \; to terminate the end of an input line within the algorithm environment. It - the semi-colon - will be printed by default, unless you issue \DontPrintSemicolon.

  • An if-statement has the syntax \If{<cond>}{<then>}, while an if-else-statement uses \eIf{<cond>}{<then>}{<else>}.

  • There is no \State with algorithm2e.