[Tex/LaTex] Foreach loop for a string

algorithm2e

how do I write

foreach triple ti in Q  do  
Q <- M 

in latex I have tried

\foreach \triple ti in {Q} {Q \gets M} 

but it doesnt work
I want the result to be displayed as follow it is a sort of an algorithm whriten in the report

"…..
Foreach Triple pattern ti ∈ Q Do

Q<- M "

    \documentclass{article}
\usepackage{amsmath}
\usepackage[linesnumbered,ruled]{algorithm2e}

\begin{document}

    \begin{algorithm}
    \SetKwInOut{Input}{Input}
    \SetKwInOut{Output}{Output}
   {MFS} $(Q,D)$\;
    \Input{ A failing Query Q = t1$\wedge$t2$\wedge$...$\wedge$tn and RDF Database D ;}
    \Output{An MFS denoted by Q* ;}
    Q $\gets$ Q* ; \\
    Q' $\gets$ Q* ; \\


    \caption{Find An MFS in a failing SPARQL Query}



\end{algorithm}
\end{document} 

I want this result

image

Best Answer

You need to use \ForEach{<condition>}{<stuff>}

enter image description here

\documentclass{article}

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

\begin{document}

\begin{algorithm}
  \caption{Find an \texttt{MFS} of a failing \texttt{SPARQL} query~$Q$}
  \SetKwInOut{Input}{inputs}
  \SetKwInOut{Output}{output}
  \SetKwProg{FindAnMFS}{FindAnMFS}{}{}

  \FindAnMFS{$(Q,D)$}{
    \Input{A failing query $Q = t_1 \wedge \dots \wedge t_n$; an \texttt{RDF} database $D$}
    \Output{An \texttt{MFS} denoted by $Q^*$}
    $Q^* \gets \emptyset$\;
    $Q' \gets Q$\;
    \ForEach{triple pattern $t_i \in Q$}{%
      $Q' \gets Q' - t_i$\;
      \If{$[[Q' \wedge Q^*]]_D \neq \emptyset$}{%
        $Q^* \gets Q^* \wedge t_i$\;
      }
    }
    \KwRet{$Q^*$}\;
  }
\end{algorithm}

\end{document} 

I've also defined a program called \FindAnMFS to obtain the outer block.