How to adjust nested if statements in the algpseudocode package

algorithmsalgpseudocodepseudocode

I have the following code:

\documentclass{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Main D Algorithm}\label{alg:cap}
\begin{algorithmic}[1]
\For{a for statement}
\If{some coding}
    \State do something
\ElsIf{another condition}
    \For{some condition}
    \If{another statement}
    \State do something
\ElsIf{problem is here}
\State whatever
\end{algorithmic}
\end{algorithm}
\end{document}

It is showing like this:

output

My problem is that I want the last elseif statement (line 8) to align with my first if statement (line 2), I am not able to do this. I tried things like using endif (obviously I modified my \usepackage then) but the algorithm didn't adjust
Any help on how to adjust this and indentations in general in this package?

Best Answer

You have to close all \For loops and \If statements by \EndFor and \EndIf, respectively.

enter image description here

\documentclass{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Main D Algorithm}\label{alg:cap}
\begin{algorithmic}[1]
\For{a for statement}
    \If{some coding}
        \State do something
    \ElsIf{another condition}
        \For{some condition}
            \If{another statement}
                \State do something
            \EndIf
        \EndFor
    \ElsIf{problem is here}
        \State whatever
    \EndIf
\EndFor
\end{algorithmic}
\end{algorithm}
\end{document}
Related Question