My googling didn't come up with how to do a switch statement in an algorithm using the algorithm and algorithmic packages, but I'm assuming you can. Most guides just didn't mention it either way.
\begin{algorithm}
\caption{send(...) method}
\begin{algorithmic}
\IF{dest equals..}
%\SWITCH{nature}
\STATE cast data...
\STATE extract data...
\STATE copy...
%\ENDSWITCH
\ELSE
\STATE match dest....
%\SWITCH{nature}
\STATE cast data...
\STATE extract data...
\STATE send...
%\ENDSWITCH
\ENDIF
\end{algorithmic}
\end{algorithm}
Thanks!
I wrote the following definitions in my latex document. It seems that they work. Just insert the above lines anywhere after your inclusion statement of the algorithmic package. Especially, to make the algorithm presentation concise, I distinguish between compound cases and one-line cases. The one-line cases begin with \CASELINE. The compound cases begin with \CASE and end with \ENDCASE. Similar to the default statements.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% The following definitions are to extend the LaTeX algorithmic
%% package with SWITCH statements and one-line structures.
%% The extension is by
%% Prof. Farn Wang
%% Dept. of Electrical Engineering,
%% National Taiwan University.
%%
\newcommand{\SWITCH}[1]{\STATE \textbf{switch} (#1)}
\newcommand{\ENDSWITCH}{\STATE \textbf{end switch}}
\newcommand{\CASE}[1]{\STATE \textbf{case} #1\textbf{:} \begin{ALC@g}}
\newcommand{\ENDCASE}{\end{ALC@g}}
\newcommand{\CASELINE}[1]{\STATE \textbf{case} #1\textbf{:} }
\newcommand{\DEFAULT}{\STATE \textbf{default:} \begin{ALC@g}}
\newcommand{\ENDDEFAULT}{\end{ALC@g}}
\newcommand{\DEFAULTLINE}[1]{\STATE \textbf{default:} }
%%
%% End of the LaTeX algorithmic package extension.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
You can try the following example.
\SWITCH {$\theta$}
\CASE {1}
\STATE Hello
\ENDCASE
\CASELINE {2}
\STATE Good-bye
\DEFAULT
\STATE Again ?
\ENDDEFAULT
\ENDSWITCH
Farn Wang Dept. of Electrical Eng. National Taiwan University
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With