20

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}
25

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}
  • this answer is helpful. – Primo Raj Jan 16 at 3:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.