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

\caption{Euclid’s algorithm}
\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

    \State \textbf{return} $L$

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.

  • 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. Dec 3, 2017 at 11:03
  • Even after changing that it's not showing the desired effect. Dec 3, 2017 at 11:07
  • Now the error telling "undefiened control sequence" for if-else block 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. Dec 3, 2017 at 11:14

1 Answer 1


You're mixing algorithmic and algpseudocode syntax.



\caption{Euclid’s algorithm}
\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
    \State \textbf{return} $L$


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

enter image description here

  • what is the meaning of "mixing algorithmic and algpseudocode syntax" ? Can you please explain where I did that ? 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.
    – egreg
    Dec 3, 2017 at 11:36
  • What is the difference between \usepackage{algorithm,algpseudocode} and \usepackage{algorithm}\usepackage{algpseudocode} ? @egreg Dec 3, 2017 at 11:46
  • @thepurpleowl None.
    – egreg
    Dec 3, 2017 at 11:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .