# Else If is not working on algorithm in algpseudocode

I tried to add \Else in my pseudocode using packages algorithm and algpseudocode but it is not showing and I am getting

Missing number, treated as zero.


This is my code and the current outcome.

\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

\begin{algorithm}
\caption{Homomorphism: Query Containment}\label{alg:euclid}
\begin{algorithmic}[1]
\Function{getHomorphism}{XPath $p_3$, XPath $q_3$}
\ForAll {nodes.getLength}
\State ($q_3(root)$ == $p_3(root)$) $\leftarrow$ \textbf{true}
\State \textbf{return} true
\If{true}
\ForAll {descendantNodes.getLength}
\State \textbf{return} true
\If {true}
\ForAll {childNodes.getLength}
\State \textbf{return} true
\If {true}
\ForAll {wildcardNodes.getLength}
\State \textbf{return} true
\Else
\State \textbf{return} false
\EndIf
\EndFunction
\end{algorithmic}
\end{algorithm}


You are using

\usepackage[noend]{algpseudocode}


That means you get no end signs in your printed algorithm. That is -- while writing your algorithm -- very problematic, because you can not proof if you added all closing end statements for your opened for, if etc.

But -- if printed ends or not -- you have to write in the tex code for your algorithm the closing ends, otherwise TeX throws error messages, because it counts opening and closing statements and throws an error if there is a missmatch!

So I recommend to delete/comment [noend] when calling the package.

Because you did not describe what your algorithm should do I can only guess what you are doing. I tried to guess well, but please check my code and correct the place for closing ends if needed!

At last I suggest to use an editor showing you small lines to be able to localize corresponding open and end markings in your algorithm (see red arrows in the following image):

Now at last I recommend to use indents to mark correspondings open and closing signs as shown in the following assumed code for your algorithm:

\Function{getHomorphism}{XPath $p_3$, XPath $q_3$}
\ForAll {nodes.getLength}
\State ($q_3(root)$ == $p_3(root)$) $\leftarrow$ \textbf{true}
\State \textbf{return} true
\If{true}
\ForAll {descendantNodes.getLength}
\State \textbf{return} true
\If {true}
\ForAll {childNodes.getLength}
\State \textbf{return} true
\If {true}
\ForAll {wildcardNodes.getLength}
\State \textbf{return} true
\EndFor
\EndIf
\EndFor
\EndIf
\EndFor
\Else % \If{true} % <=============================================
\State \textbf{return} false
\EndIf
\EndFor
\EndFunction


If the algorithm is complicated you can run into problems with the "dangling else". To make clear where the else belongs just add an comment with the corresponding If clause as I showed in the algorithm code marked with <========.

All that helps you to have an overview of your algorithm.

Now supposing I guessed right you have the final tex code

\documentclass{article}

\usepackage{algorithm}
\usepackage{algpseudocode} % <================= better without [noend]

\begin{document}
\begin{algorithm}
\caption{Homomorphism: Query Containment}\label{alg:euclid}
\begin{algorithmic}[1]
\Function{getHomorphism}{XPath $p_3$, XPath $q_3$}
\ForAll {nodes.getLength}
\State ($q_3(root)$ == $p_3(root)$) $\leftarrow$ \textbf{true}
\State \textbf{return} true
\If{true}
\ForAll {descendantNodes.getLength}
\State \textbf{return} true
\If {true}
\ForAll {childNodes.getLength}
\State \textbf{return} true
\If {true}
\ForAll {wildcardNodes.getLength}
\State \textbf{return} true
\EndFor
\EndIf
\EndFor
\EndIf
\EndFor
\Else
\State \textbf{return} false
\EndIf
\EndFor
\EndFunction
\end{algorithmic}
\end{algorithm}
\end{document}


resulting in the following printed algorithm:

The advantage now is you can simply proof and see where the Ifs, For, Whiles etc. ends in the alporithm.

If you now (after the final proof everything is okay) add [noend] again you get:

If that algorithm is better to read you have to decide by your own, I always prefer the print with corresponding end statements -- but that is my personal opinion!

There are other packages for generating algorithm (see ctan) you can try, then you can get printed lines in the algorithm like in the screenshot of my editor. Could it make easier to locate corresponding start and end marks ...

You are missing lots of end statements. Adding them does not throw the error anymore.

I have added some indentation to make it easier to spot nested blocks. Please note that due to your missing end statements the output might differ from what you expect. I chose one of the possible ways to end the nested blocks.

\documentclass{article}

\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

\begin{document}

\begin{algorithm}
\caption{Homomorphism: Query Containment}\label{alg:euclid}
\begin{algorithmic}[1]
\Function{getHomorphism}{XPath $p_3$, XPath $q_3$}
\ForAll {nodes.getLength}
\State ($q_3(root)$ == $p_3(root)$) $\leftarrow$ \textbf{true}
\State \textbf{return} true
\If{true}
\ForAll {descendantNodes.getLength}
\State \textbf{return} true
\If {true}
\ForAll {childNodes.getLength}
\State \textbf{return} true
\If {true}
\ForAll {wildcardNodes.getLength}
\State \textbf{return} true
\EndFor
\EndIf
\EndFor
\EndIf
\EndFor
\Else
\State \textbf{return} false
\EndIf
\EndFor
\EndFunction
\end{algorithmic}
\end{algorithm}

\end{document}