# 6.2 Multiple selection with Switch

> `Switch` looks at a value only once and jumps straight to the branch whose label matches.

Web page: https://pseudolearn.app/en/spec/control/segun

> **In one sentence**
>
> `Switch` looks at a value only once and jumps straight to the branch whose label matches.

## 6.2.1 How to write it

```pseudolearn
Switch «selector» Do
  «etiqueta», «etiqueta»:
    «instrucciones»
  ⟦Otherwise:
    «instrucciones»⟧
EndSwitch
```

> **Parts**
>
> - `Switch` opens the selection and «selector» is the value being looked at: an integer, an enumeration or a variant.
> - `Do` separates the selector from the branches.
> - Each branch starts with one or more «labels» separated by commas and ends its header with `:`.
> - `Otherwise:` is optional, unique and goes last. It collects whatever matches no label.
> - `EndSwitch` closes the selection.

## 6.2.2 How it works

The selector is computed **once**. Then the branch with that label is found, run, and the selection ends: there is no "fall-through" to the next branch. If none matches and there is no `Otherwise`, nothing is done.

### Program: Is there school today?

```pseudolearn
Algorithm DiaSemana
  Define dia As Integer <- 6;
  Switch dia Do
    1, 2, 3, 4, 5:
      Write "Hay clase";
    6, 7:
      Write "Fin de semana";
    Otherwise:
      Write "Ese día no existe";
  EndSwitch
EndAlgorithm
```

**Output**

```text
Fin de semana
```

*The program finishes normally.*

**Figure 6.2-1: The branches of Switch**

A Switch on dia with three branches: 1 to 5 school day, 6 and 7 weekend, and Otherwise. The selector is 6 and only the second branch is followed down to EndSwitch.

The selector is 6, so the engine jumps to the branch holding that label. The others are not even looked at.

1. Step 1: The selector is computed only once: `dia` is 6.
2. Step 2: 6 is in the second branch: its `Write` runs.
3. Step 3: When the branch ends the `Switch` is left, without going through the others.

Labels are **integer literals** (signed if needed) or **members of an enumeration**. Reals, characters, texts and booleans are not valid selectors. A label cannot be repeated.

## 6.2.3 With enumerations

If the selector is an enumeration, the branches must cover **all** its members, or include `Otherwise`. That way the engine warns you when a new member is added and nobody handles it.

### Program: An incomplete traffic light

```pseudolearn
Enum Semaforo
  Rojo, Ambar, Verde
EndEnum
Algorithm Cruce
  Define luz As Semaforo <- Semaforo.Ambar;
  Switch luz Do
    Semaforo.Rojo:
      Write "Para";
    Semaforo.Verde:
      Write "Pasa";
  EndSwitch
EndAlgorithm
```

**Diagnostics**
- `nonExhaustiveSwitch` (error, 6:3): Missing branches: Semaforo.Ambar

*The program finishes with an error.*

The message names the missing members, in the order they were declared. `Switch` also opens variants such as `Optional` and `Result`: that is explained in [Optional and Result](https://pseudolearn.app/en/spec/tipos/opcional-resultado.md).

> **Common mistakes**
>
> ### Program: A repeated label
>
> ```pseudolearn
> Algorithm Repetida
>   Define n As Integer <- 1;
>   Switch n Do
>     1:
>       Write "uno";
>     1:
>       Write "otra vez uno";
>   EndSwitch
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `duplicateSwitchCaseLabel` (error, 6:5): Duplicate label in switch statement: 1
>
> *The program finishes with an error.*
>
> ### Program: A text selector
>
> ```pseudolearn
> Algorithm TextoSel
>   Define t As String <- "a";
>   Switch t Do
>     "a":
>       Write "a";
>   EndSwitch
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `incompatibleSwitchCaseType` (error, 4:5): Case label type is not compatible with switch selector
>
> *The program finishes with an error.*

> **Under the hood**
>
> The selector is kept in a temporary and every branch is a comparison with a jump. Since there is no fall-through, each branch ends with a direct jump to the block after `EndSwitch`.
>
> **Intermediate representation**
>
> ```text
> module DiaSemana en
> func suspendible @main() -> unit {
> b0() mass 2:
>   stmt.enter @2:3-2:29
>   %0 = const.i64 6 @2:28-2:29
>   stmt.exit @2:3-2:29
>   stmt.enter @3:3-10:12
>   switch %0, b2(), 1, b3(), 2, b3(), 3, b3(), 4, b3(), 5, b3(), 6, b4(), 7, b4()
> b1() mass 0:
>   stmt.exit @3:3-10:12
>   return @11:1-11:13
> b2() mass 1:
>   decide.default @3:3-10:12
>   stmt.enter @9:7-9:32
>   %1 = const.str "Ese día no existe" @9:13-9:32
>   out.write %1, newline @9:7-9:32
>   stmt.exit @9:7-9:32
>   jump b1()
> b3() mass 1:
>   %2 = const.i64 0
>   decide.case %2 @3:3-10:12
>   stmt.enter @5:7-5:24
>   %3 = const.str "Hay clase" @5:13-5:24
>   out.write %3, newline @5:7-5:24
>   stmt.exit @5:7-5:24
>   jump b1()
> b4() mass 1:
>   %4 = const.i64 1
>   decide.case %4 @3:3-10:12
>   stmt.enter @7:7-7:28
>   %5 = const.str "Fin de semana" @7:13-7:28
>   out.write %5, newline @7:7-7:28
>   stmt.exit @7:7-7:28
>   jump b1()
> }
> ```

> **Note**
>
> `Switch` and a chain of nested `If`s do the same with concrete values. Rewrite `dia-semana` with `If` and compare which reads better and which catches an omission sooner.

**In short**
- `Switch` computes the selector once and runs a single branch.
- Labels: integers or enumeration members, never repeated.
- No fall-through between branches; `Otherwise` is optional.
- With enumerations, the branches must cover every member.

**See also**
- [6.1 The If decision](https://pseudolearn.app/en/spec/control/si.md)
- [3.8 Enumerations](https://pseudolearn.app/en/spec/tipos/enumerados.md)
- [3.10 Optional and Result](https://pseudolearn.app/en/spec/tipos/opcional-resultado.md)
