[Tex/LaTex] if-else inside for loop in algorithm package

algorithmicalgorithms

I am trying for a if-else statement inside a for loop. This my code and there is some problem with inner for loop and if-else statement. Code is as follows

\begin{algorithm}
\caption{Euclid’s algorithm}
\label{alg:euclid}
\begin{algorithmic}[1]
\State U: a set of authentic users
\State T: a set containing trustor and trustee information
    \Procedure{DepthDetection}{$T_t_h, U_i$}
    % \State $r\gets a\bmod b$
    \State $T_u_i  \gets list\ of\ trustee\ taking\ U_i\ as\ trustor$
    \State $I_u_i  \gets list\ of\ items\ rated\ by\ U_i$
    \State Build ${U_i}'s\  trust\ network\ using\ $T_t_h$

    \For{each item $i$ in $I_u_i$ }
    \State $NU_u_i  \gets list\ of\ users\ who\ rated\ I_i$
        \For{each user $u$ in $NU_u_i$ }
            \IF{$u$ is in $T_u_i$}
                \STATE use the rating and trust of $u$ for prediction of rating
            \ENDIF
        \EndFor
    \EndFor

    \State \textbf{return} $L$
    \EndProcedure
\end{algorithmic}\end{algorithm}  

And the output I get is
enter image description here

As you can see there is a continuation of if condition and statements after the inner loop. I want that to be in nested format.

Best Answer

You're mixing algorithmic and algpseudocode syntax.

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

\begin{document}

\begin{algorithm}
\caption{Euclid’s algorithm}
\label{alg:euclid}
\begin{algorithmic}[1]
\State $U$: a set of authentic users
\State $T$: a set containing trustor and trustee information
    \Procedure{DepthDetection}{$T_{t_h}$, $U_i$}
    \State $T_{u_i} \gets$ list of trustee taking $U_i$ as trustor
    \State $I_{u_i} \gets$ list of items rated by $U_i$
    \State Build $U_i$'s trust network using $T_{t_h}$
    \For{each item $i$ in $I_{u_i}$}
    \State $NU_{u_i} \gets$ list of users who rated $I_i$
        \For{each user $u$ in $NU_{u_i}$ }
            \If{$u$ is in $T_{u_i}$}
                \State use the rating and trust of $u$ for prediction of rating
            \EndIf
        \EndFor
    \EndFor
    \State \textbf{return} $L$
    \EndProcedure
\end{algorithmic}
\end{algorithm}

\end{document}

I also fixed misuses of math mode and the double subscripts.

enter image description here

Related Question