Skip to content

Switch-case statement

Use a switch/case statement in variables environments to make sure your variables satisfy specific constraints

\begin{variables}
  \randint{dice}{1}{3}
  \begin{switch}

    \begin{case}{dice=1}
      <variable definitions>
    \end{case}

    \begin{case}{dice=2}
      <variable definitions>
    \end{case}

    ...

    \begin{default}
      <variable definitions>
    \end{default}

  \end{switch}
\end{variables}
  • Every variable in a switch environment must have a default definition, but not a definition for every single case environment. (see example below)

  • There is no limitation on how many case environments you can use inside a switch environment. The default environment though is obligatory!

  • You can use switch/case statements globally and on question level.

Never use switch/case statements in combination with \randadjustIf. It is meant as an alternative.

Example 1

\begin{variables}
  \randint{a}{1}{5}
  \function{f0}{10*a}

  \begin{switch}
    \begin{case}{a>3}
      \number{c}{3}
      \randrat{d}{1}{2}{3}{7}
      \drawFromSet{m}{1,2,3}
      \function{f}{2*a}
      \derivative{g}{3x^2+sqrt(x)}{x}
      \substitute{h}{sqrt(y)}{y}{g}
      \string{s}{case 1}
    \end{case}

    \begin{case}{a=3}
      \number{c}{5}
    \end{case}

    \begin{default}
      \randint{c}{1}{10}
      \randrat{d}{-2}{-1}{3}{7}
      \drawFromSet{m}{-1,-2,-3}
      \function{f}{-2*a}         
      \derivative{g}{-3x^2+sqrt(x)}{x}   
      \substitute{h}{sqrt(y)}{y}{g}
      \string{s}{default case}
    \end{default}

  \end{switch}

\end{variables}

Example 2

\begin{problem}
  \begin{variables}
    \randint[Z]{s}{-1}{1}
    \begin{switch}
      \begin{case}{s>0}
        \string{s_equation}{
          \begin{equation*}
            \int \cos(x)\cdot e^x\,dx.
          \end{equation*}            
        }
      \end{case}          
      \begin{default}
        \string{s_equation}{
          \begin{equation*}
            \int \sin(x)\cdot e^x\,dx.
          \end{equation*}
        }
      \end{default}
    \end{switch}
    \function{f1}{1/2*exp(x)*(sin(x)+s*cos(x))}
  \end{variables}

  \begin{question}\type{input.function}    
    \text{
      Lösen Sie das folgende unbestimmte Integral mittels partieller Integration
      \\
      \var{s_equation}
      \\
      Die Lösung lautet $F(x)= $ \ansref $+\ c$.
    }       
    \begin{answer}
      \solution{f1}
      \inputAsFunction{x}{A0}
      \checkFuncForZero{A0-f1}{-1}{1}{10}
      \end{answer}
  \end{question}
\end{problem}