# Multi-Way Selection (CASE OF ... OTHERWISE ... ENDCASE)

- **Track:** Cambridge IGCSE™ Computer Science (0478 / 0984) (CAMBRIDGE IGCSE 0478)
- **Module:** Section 8: Programming Concepts & File Handling
- **Estimated Duration:** 10 minutes
- **Access:** Open Theory (All-Access for Interactive IDE &amp; Autotests)

---

## Theory & Conceptual Background

When an algorithm must branch across many mutually exclusive outcomes based on the value of a single variable, nesting dozens of `IF ... THEN ... ELSE` statements creates messy, hard-to-read code. Cambridge pseudocode provides the **`CASE OF`** statement for elegant multi-way branching.

---

## 1. Syntax of `CASE OF`

```pseudocode
CASE OF <Identifier>
    <Value1> : <Statement>
    <Value2> : <Statement>
    <LowValue> TO <HighValue> : <Statement>
    <ValA>, <ValB>, <ValC> : <Statement>
    OTHERWISE : <Statement>
ENDCASE
```

### Key Syntax Rules:

1. **Target Variable**: Specified once in the `CASE OF <Identifier>` header.
2. **Selector Colons**: Each matching value is followed immediately by a colon `:`.
3. **Range Support**: You can test inclusive ranges using the keyword `TO` (e.g. `1 TO 10 : ...`).
4. **List Support**: You can test comma-separated discrete values (e.g. `'A', 'E', 'I', 'O', 'U' : ...`).
5. **Fallback (`OTHERWISE`)**: Catches any input value that did not match any of the prior branches.
6. **Closing (`ENDCASE`)**: Required to terminate the structure.

---

## 2. Comparison: Nested `IF` vs `CASE OF`

  
| Feature | Nested `IF` | `CASE OF` |
| --- | --- | --- |
| **Best Used For** | Complex conditions involving multiple variables, relational operators (`>`, `<`), and compound logic (`AND`, `OR`). | Inspecting a single variable against specific constants or discrete ranges. |
| **Readability** | Tends to drift rightwards with deep indentation ("pyramid of doom"). | Clean vertical alignment of branches. |
| **Default Handling** | Final `ELSE` block. | `OTHERWISE` block. |
| **Termination Keyword** | Multiple `ENDIF` statements matching each opened `IF`. | Single `ENDCASE` statement. |

---

## Interactive Exercises & Autotest Specifications

### Exercise 1: Exam-Style Check: CASE OF Range and Fallback Syntax (Multiple choice, 25 XP)

**Instructions:**
Which keyword is used in Cambridge pseudocode as the catch-all fallback clause in a `CASE OF` statement when none of the specified values or ranges match?

**Options:**
- **A**: DEFAULT
- **B**: ELSE
- **C**: OTHERWISE
- **D**: CATCH


**Autotest Expectations:**
- **Fallback keyword identified**: `selection_match` -> Expected: `opt_otherwise`

### Exercise 2: Exam-Style Question: Trace CASE OF Evaluation (Trace table, 35 XP)

**Instructions:**
### Task

Trace the execution of this CASE OF construct:

```pseudocode
CASE OF DayNumber
    1, 2, 3, 4, 5 : Status <- "Weekday"
    6, 7          : Status <- "Weekend"
    OTHERWISE     : Status <- "Invalid"
ENDCASE
OUTPUT "Status: ", Status
```

Trace the output for three test values: `DayNumber = 3`, `DayNumber = 7`, and `DayNumber = 9`. Complete the trace table rows.


**Starter Code:**
```text
{"columns": ["DayNumber", "Status", "OUTPUT"], "rows": [["", "", ""], ["", "", ""], ["", "", ""]]}
```

**Autotest Expectations:**
- **CASE OF statement trace matches expected values**: `table_match` -> Expected: ``

### Exercise 3: Independent Practice: Signal messages (Pseudocode, 30 XP)

**Instructions:**
Read RED, AMBER or GREEN. Output STOP, WAIT or GO respectively. Output INVALID for any other string.

Write a complete program. Read inputs in the stated order. Output only the requested results, one per line, without input prompts. Test with your own values before submitting.


**Starter Code:**
```text
// Plan your solution, then write your program.

```

**Autotest Expectations:**
- **RED**: `test_case` -> Expected: `{&quot;inputs&quot;:[&quot;RED&quot;],&quot;output&quot;:&quot;STOP&quot;}`
- **AMBER**: `test_case` -> Expected: `{&quot;inputs&quot;:[&quot;AMBER&quot;],&quot;output&quot;:&quot;WAIT&quot;}`
- **GREEN**: `test_case` -> Expected: `{&quot;inputs&quot;:[&quot;GREEN&quot;],&quot;output&quot;:&quot;GO&quot;}`
- **BLUE**: `test_case` -> Expected: `{&quot;inputs&quot;:[&quot;BLUE&quot;],&quot;output&quot;:&quot;INVALID&quot;}`

