39

How can I write the else if block with condition with the usepackage algorithm2e?

I want to write the following with the algorithm2e package:

if(condition){
  //code
 }else if(condition){
  // code
 }else{
  //code
 }

Code:

\documentclass{article}
\usepackage{german,t1enc}
\usepackage{lipsum}
%Header part
\usepackage{fancyhdr}
\pagestyle{fancy}
%\cfoot{center of the footer!}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}

%pseudocode.
\usepackage[linesnumbered,ruled,vlined]{algorithm2e} 

\usepackage[dvipsnames]{xcolor}
\renewcommand{\baselinestretch}{1.5}
%blacksquare
\usepackage{ amssymb }

\begin{document}


\begin{algorithm}[H]


function search(v,w,s) \\
   binarsearch(v,w,s,0, |w|) \\

   binarsearch(v,w,s,l, r) \\

   \If{$v$ prefix of $w_s[m]$}{
      return true;
   }\ElseIf(elseif comment){
                           binarsearch(v,w,s,l,m-1)
                           }  
\caption{}
\label{alg: initializer}
\end{algorithm}

1 Answer 1

55

To combine such conditions, write each as a separate component using \uIf and \uElseIf (and \uElse, if needed):

enter image description here

\documentclass{article}

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

\begin{document}

\begin{algorithm}[H]
  \uIf{if condition}{
    something if \;
  }
  \uElseIf{elseif condition}{
    something elseif \;
  }
  \Else{
    something else \;
  }
\caption{An algorithm}
\end{algorithm}

\end{document}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .