# Write down the algorithm

I am trying to write down my algorithm and faked I wrote Foreach loop. Otherwise there is no command \Foreach. The followingmy program could not be executed. Also I use package \usepackage{algpseudocode} and \usepackage{algorithm}.

\begin{algorithm}
\caption{Algorithm for generating atn.json file}
\begin{algorithmic}
\Statex \Comment { \textbf{Input:} ListofOrderedSelectedJobs, testDuration, beginDate, endDate, MapDeviceTranmissionTime, \textbf{Output:} atn.json
\For{\textbf{each} selectJob \textit{in} ListofOrderedSelectedJobs \textit{do}}
\Statex\Comment{ \%comment: Do the same process for group B, C and D devices\%}
\EndIf
\If {$selectJob == 'U'$ \textit{then}}
\For{\textbf{each} device \in choosenDevice \textit{do}}
\State  MapDeviceTranmissionTime.put(device,beginDate)
\Statex\Comment{ \%comment: Put begin test date for each device and store that value in the map\%}
\If {$selectJobType == 'Z'$ \textit{then}}
\If { randomTransmissionDate \textit{exists} \textit{then} readRandomExecutionDate() }
\EndIf
\EndIf
\EndFor
\If { $selectJobInterval \leq testDuration$ \textit{then}}
\State $times = testDuration / selectJobInterval$
\Else
\State $times = (testDuration / selectJobInterval) + 1$
\EndIf
\For{$i = 1$ to times \textit{do}}
\If {$selectJob == 'U'$ \textit{then}}
\For{\textbf{each} device \in choosenDevice \textit{do}}
\State timesDate = timesDate + Interval
\State MapDeviceTranmissionTime.put(device,timesDate)
\Statex\Comment{ \%comment: Create URL string and encode it by using timesDate date\%}
\If {$selectJobType == 'Z'$ \textit{then}}
\If { randomTransmissionDate \textit{exists} \textit{then} readRandomExecutionDate() }
\EndIf
\EndFor
\EndIf
\EndFor
\EndFor
\end{algorithmic}
\end{algorithm}


• any help or idea? – limonik Apr 9 '16 at 22:50
• It is not clear what are you asking for. At least not to me. – wilx Apr 9 '16 at 22:51
• @wilx when I put this in my tex file it gave error messages and when I click error messages it leads me another file. Since this is my thesis I used different .tex file for each chapter. Therefore I could not understand the root of the error. Could you pleas check my code – limonik Apr 9 '16 at 22:55

Your problem is that you are missing some closing \EndIf and \EndFor. I have made some changes to your code and indented it properly and I am able to compile it. Also, you are not using the package provided macros correctly.

\documentclass{article}

\usepackage{algpseudocode}
\usepackage{algorithm}

\begin{document}
\begin{algorithm}
\caption{Algorithm for generating atn.json file}
\begin{algorithmic}
\Statex{\Comment{\textbf{Input:} ListofOrderedSelectedJobs, testDuration,
beginDate, endDate, MapDeviceTranmissionTime; \textbf{Output:}
atn.json}}
\Statex
\ForAll{selectJob $\in$ ListofOrderedSelectedJobs}
\Statex\Comment{\%comment: Do the same process for group B, C and D devices\%}
\EndIf
\If{$selectJob == 'U'$}
\ForAll{device $\in$ choosenDevice}
\State MapDeviceTranmissionTime.put(device,beginDate)
\Statex\Comment{\%comment: Put begin test date for each device and store that value in the map\%}
\If{$selectJobType == 'Z'$}
\If{$\exists$ randomTransmissionDate}
\Else
\EndIf
\EndIf
\EndFor
\EndIf
\If{ $selectJobInterval \leq testDuration$}
\State $times = testDuration / selectJobInterval$
\Else
\State $times = (testDuration / selectJobInterval) + 1$
\EndIf
\For{$i = 1 \to times$}
\If {$selectJob == 'U'$}
\ForAll{device $\in$ choosenDevice}
\State timesDate = timesDate + Interval
\State MapDeviceTranmissionTime.put(device,timesDate)
\Statex\Comment{ \%comment: Create URL string and encode it by using timesDate date\%}
\If{$selectJobType == 'Z'$}
\If{$\exists$ randomTransmissionDate}