# How to write nested if else statement inside function

I am trying to write the following algorithm in LaTex using algorithm2e package.

\IncMargin{1em}
\begin{algorithm}
\caption{An example}
\SetNoFillComment
\DontPrintSemicolon
\BlankLine
\SetKwInOut{Input}{Input}\SetKwInOut{Output}{Output}
\Input{set ($x$), $y$, $Q$, $x_t$, epsilon (\varepsilon)}
\Output{$G_r$}
\BlankLine
$x$ = \{$x_1$, $x_2$,\dots\}; $x_t$ \in $x$\;
\\$y$ = \{$y_1$, $y_2$,\dots\}\;
\SetKwFunction{Fmain}{main}
\SetKwProg{Fn}{Function}{:}{end}
\Fn{\Fmain{}}{
\eIf {condition 1}
{do this\;
do this\;
\eIf {condition 11}
{$x_t$ \leftarrow $\max\limits_y Q(x_t,.)$\;}
{x_t \leftarrow  $rand$\quad$num$\;}
$G_u$ =$x_t$\;
}
{$G_u$ = 0\;}
return $G_u$\;}
\end{algorithm}


But the above code gave the following output. Would anyone please help to correct my code? Thank you.

You get many (really many) errors with that input, due to inconsistent usage of $ to delimit math formulas. \documentclass{article} \usepackage{algorithm2e} \begin{document} \IncMargin{1em} \begin{algorithm} \caption{An example} \SetKwInOut{Input}{Input} \SetKwInOut{Output}{Output} \SetKwFunction{Fmain}{main} \SetKwProg{Fn}{Function}{:}{end} \SetNoFillComment \DontPrintSemicolon \BlankLine \Input{set ($x$),$y$,$Q$,$x_t$, epsilon ($\varepsilon$)} \Output{$G_r$} \BlankLine$x = \{x_1, x_2,\dots\}$;$x_t\in x$\;$y = \{y_1, y_2,\dots\}$\; \BlankLine \Fn{\Fmain{}}{ \eIf {condition 1}{ do this\; do this\; \eIf {condition 11} {$x_t \leftarrow \max\limits_y Q(x_t,.)$\;} {$x_t \leftarrow  \mathrm{rand}\,\mathrm{num}$\;}$G_u = x_t$\; } {$G_u = 0$\;} return$G_u$\;} \end{algorithm} \end{document}  • Thanks a lot. Can I ask you one more question? How to make the rand num font italic? With {$x_t \leftarrow \mathrm{rand}\,\mathrm{num}$\;}, it is showing normal font. Thanks again. Commented Apr 1, 2022 at 9:08 • @11187162 You can do \mathit{rand\,num} Commented Apr 1, 2022 at 9:30 • many thanks. Is there any way to make max in {$x_t \leftarrow \max\limits_y Q(x_t,.)\$\;} italic? Commented Apr 1, 2022 at 9:59