3

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.

4
  • Double subscripts should be written $T_{ui}$ or $T_{u_i}$ depending the desired effect. $T_u_i$ throws an error and disturbs the rest of the typesetting. Commented Dec 3, 2017 at 11:03
  • Even after changing that it's not showing the desired effect. Commented Dec 3, 2017 at 11:07
  • Now the error telling "undefiened control sequence" for if-else block Commented Dec 3, 2017 at 11:09
  • Please complete your code to a minimal document demonstrating the problem - you have internal definitions in play that we need to see. Also check that that document does not produce errors - look in the .log file. Commented Dec 3, 2017 at 11:14

1 Answer 1

2

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

4
  • what is the meaning of "mixing algorithmic and algpseudocode syntax" ? Can you please explain where I did that ? Commented Dec 3, 2017 at 11:34
  • @thepurpleowl In the nested if statement you were using \IF, \STATE and \ENDIF which are from algorithmic, not from algpseudocode. Commented Dec 3, 2017 at 11:36
  • What is the difference between \usepackage{algorithm,algpseudocode} and \usepackage{algorithm}\usepackage{algpseudocode} ? @egreg Commented Dec 3, 2017 at 11:46
  • @thepurpleowl None. Commented Dec 3, 2017 at 11:53

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.