2

I want to use the IF else in my code using LaTeX. But I am unable to end the if-else. I have used the ENDIF and the commands like this \newcommand\sIf[2]{ \If{#1}#2\EndIf}. I want to print the END if at the end of the if-else. The following is the code and its output.

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

\begin{document}


\begin{algorithm}
\caption{Calculating Speed Using GPS}\label{alg1}
\begin{algorithmic}[1]
\State $distance\gets  0 $
\State $time\gets  0 $
\State $speedGPS  \gets  0 $


\If{$location.hasSpeed()$}
\State $speedGPS\gets location.getSpeed()$
\Else
\State $speedGPS \gets dist / time$
\EndIf


\State $counter  \gets (counter + 1) \% data_points$
\State $speedGPS  \gets speedGPS * 3.6d$
\end{algorithmic}
\end{algorithm}


\end{document}

enter image description here

Werner
  • 14,324
  • 7
  • 55
  • 77
Fizzah
  • 63
  • 1
  • 2
  • 12

1 Answers1

2

Drop the noend option you're passing to algpseudocode:

enter image description here

\documentclass{article}

\usepackage{algorithm,algpseudocode}

\begin{document}

\begin{algorithm}
  \caption{Calculating Speed Using GPS}
  \begin{algorithmic}[1]
    \State $distance\gets  0 $
    \State $time\gets  0 $
    \State $speedGPS  \gets  0 $
    \If{$location.hasSpeed()$}
      \State $speedGPS\gets location.getSpeed()$
    \Else
      \State $speedGPS \gets dist / time$
    \EndIf
    \State $counter  \gets (counter + 1) \% data_points$
    \State $speedGPS  \gets speedGPS \times 3.6d$
  \end{algorithmic}
\end{algorithm}

\end{document}
Werner
  • 14,324
  • 7
  • 55
  • 77