# `switch`{l=cs}

```{literalinclude} ../code-wi/switch.c
:language: c
:linenos:
```
:::{command-output} echo s\ne | code-wi/switch.exe
:shell:
:::
In the example above we get two times `DONE ☕`, because the newline from the first command must be consumed first by the `getchar` in line 21. So the `DONE` state is entered twice.

Prefer `switch` if you have multiple *individual* options for a variable. Choose `if-else` if you have numeric ranges.

:::{activity} State machine using `switch`
You probably implemented the last activity using an `if-else`. Now use a `switch`.
:::

<!--
also mention case 1...3:
https://en.wikipedia.org/wiki/C2Y_(C_standard_revision) ?
-->