# Generating a box inside algorithm in beamer

I would like to create two boxes (or two frames) and one arrow which goes from the center of right side of one box and do half a circle and point on the other box.

Below is an example of code:

\documentclass{beamer}
\usepackage{algorithm,algpseudocode}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{xcolor}
\makeatletter

\begin{document}

\begin{frame}
\begin{algorithm}[H]
\begin{algorithmic}
\State $y \leftarrow 1$
% Start box 1
\If{$n < 0$}
\State $X \leftarrow 1 / x$
\State $N \leftarrow -n$
\Else
\State $X \leftarrow x$
\State $N \leftarrow n$
\EndIf
%End box 1
%Start box 2
\While{$N \neq 0$}
\If{$N$ is even}
\State $X \leftarrow X \times X$
\State $N \leftarrow N / 2$
\Else[$N$ is odd]
\State $y \leftarrow y \times X$
\State $N \leftarrow N - 1$
\EndIf
\EndWhile
% End box 2
\end{algorithmic}
\end{algorithm}
\end{frame}
\end{document}


How could I create a box around the first \If condition (until \EndIf) and around while condition and then an arrow from the center of the right side of of the "if condition" to the "while condition" box?

Here's one possibility, placing marks and then using the marks to draw the boxes and the arrow:

\documentclass{beamer}
\usepackage{algorithm,algpseudocode}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,intersections}
\usepackage{xcolor}
\makeatletter

\newcommand\tikzmark{%
\tikz[overlay,remember picture,baseline] \coordinate (#1);}

\begin{document}

\begin{frame}
\begin{algorithm}[H]
\begin{algorithmic}
\State\tikzmark{start1} $y \leftarrow 1$
% Start box 1
\If{$n < 0$}
\State $X \leftarrow 1 / x$
\State $N \leftarrow -n$
\Else
\State $X \leftarrow x$
\State $N \leftarrow n$
\EndIf\tikzmark{end1}
%End box 1
%Start box 2
\Statex\vskip-2.2ex
\tikzmark{start2}\While{$N \neq 0$}
\If{$N$ is even}
\State $X \leftarrow X \times X$
\State $N \leftarrow N / 2$
\Else[$N$ is odd]
\State $y \leftarrow y \times X$
\State $N \leftarrow N - 1$
\EndIf
\EndWhile\tikzmark{end2}
% End box 2
\end{algorithmic}
\end{algorithm}

\begin{tikzpicture}[remember picture,overlay]
\draw[orange] ( $(start1) + (-2pt,-2pt)$ ) rectangle ( $(end1) + (1.3,-2pt)$ );
\draw[orange] ( $(start2) + (-2pt,-2pt)$ ) rectangle ( $(end2) + (1.8,-2pt)$ );
\draw[orange,-latex] let \p1=(start1), \p2=(end1), \p3=(start2), \p4=(end2)
in ( $(\x2,0.5*\y1+0.5*\y2) + (1.3,0)$ ) to[out=0,in=0] ( $(\x4,0.5*\y3+0.5*\y4) + (1.8,0)$ );
\end{tikzpicture}
\end{frame}

\end{document} 