[Tex/LaTex] Switch statement in LaTeX using package pseudocode

pseudocode

I am using \usepackage{pseudocode} in LaTeX for writing pseudocode.

How to write switch statement using this package?

I write more 90% pseudocode using the package pseudocode. In function Validate i want to write switch Statement.

\PROCEDURE{Validate}{location1,location2}
l1 \GETS 0 \\
l2 \GETS 0 \\
validate \GETS true \\
\IF board[location1] \neq 0 \THEN
    \BEGIN 
    l1 \GETS board[location1]
    l2 \GETS board[location2]
    \IF l1 + l2 is odd and l2 \neq 0 \THEN
        validate \GETS false
    \ELSE
        \BEGIN
            %\SWITCH  here i want to write switch statement
                   %\BEGIN

            %\END
        \END
    \END

 \RETURN {validate}\\
 \ENDPROCEDURE

I can find add switch statement in pseudocode using other packages like algorithmicx and algorithm. There is any way to add switch statement using pseudocode package ?

Best Answer

I'm really not sure whether this is what you're after, but it's a first go:

enter image description here

\documentclass{article}
\usepackage{amsmath,pseudocode}% http://ctan.org/pkg/{amsmath,pseudocode}
\newcommand{\algvar}[1]{\text{\ttfamily\upshape#1}}
\makeatletter
\newcommand{\SWITCH}{\mbox{\bfseries switch }}
\renewcommand{\CASE}{\mbox{ \bfseries case }}
\newcommand{\CASEELSE}{\mbox{ \bfseries else }}
\makeatother
\begin{document}
\begin{pseudocode}{Validate}{\algvar{location1}, \algvar{location2}}
    \PROCEDURE{Validate}{\algvar{location1}, \algvar{location2}}
    l_1 \GETS 0 \\
    l_2 \GETS 0 \\
    \algvar{validate} \GETS \TRUE \\
    \IF \algvar{board}[\algvar{location1}] \neq 0 \THEN
        \BEGIN 
        l_1 \GETS \algvar{board}[\algvar{location1}] \\
        l_2 \GETS \algvar{board}[\algvar{location2}] \\
        \IF (l_1 + l_2 \text{ is odd}) \AND (l_2 \neq 0) \THEN
            \algvar{validate} \GETS \FALSE
        \ELSE
            \BEGIN
                \SWITCH i
                    \BEGIN
                        \CASE 1 \text{ Something 1} \\
                        \CASE 2 \text{ Something 2} \\
                        \CASE 3 \text{ Something 3} \\
                        \CASE 4 \text{ Something 4} \\
                        \CASEELSE \text{ Something else}
                    \END
            \END
        \END \\
        \RETURN {\algvar{validate}} \\
    \ENDPROCEDURE
\end{pseudocode}
\end{document}

The constructions used by pseudocode is based on arrays, so it's easily modifiable. However, it depends on what you're after in terms of the look of a \SWITCH command, in terms of placement, alignment and justification.

Related Question