# Selection & Decision Making (IF ... THEN ... ELSE)

- **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

**Selection** is a fundamental programming construct that allows an algorithm to choose alternative execution paths based on whether a conditional expression evaluates to `TRUE` or `FALSE`.

---

## 1. Single and Two-Way Selection

### Syntax:

```pseudocode
IF <Condition> THEN
    // Code executed when Condition is TRUE
ELSE
    // Code executed when Condition is FALSE
ENDIF
```

- The `ELSE` clause is optional if no action is required when the condition is false.
- Every `IF` statement **must terminate with a matching `ENDIF`**.

---

## 2. Multi-Way Nested Selection

When selecting between three or more outcomes, `IF` statements can be nested inside the `ELSE` branch:

```pseudocode
IF Mark >= 80 THEN
    Grade <- "A"
ELSE
    IF Mark >= 70 THEN
        Grade <- "B"
    ELSE
        IF Mark >= 60 THEN
            Grade <- "C"
        ELSE
            Grade <- "U"
        ENDIF
    ENDIF
ENDIF
```

Notice that three opened `IF` statements require three matching `ENDIF` statements at the close.

---

## 3. Logical Operators in Conditions

Compound conditions combine multiple relational comparisons using Boolean operators:

  
| Operator | Meaning | Example |
| --- | --- | --- |
| `AND` | Evaluates to `TRUE` only if **both** operands are true. | `(Age >= 18) AND (HasDrivingLicense = TRUE)` |
| `OR` | Evaluates to `TRUE` if **at least one** operand is true. | `(IsWeekend = TRUE) OR (IsHoliday = TRUE)` |
| `NOT` | Inverts the logical state (`TRUE` becomes `FALSE`, `FALSE` becomes `TRUE`). | `NOT (AccountLocked = TRUE)` |

---

## Interactive Exercises & Autotest Specifications

### Exercise 1: Exam-Style Check: Logical Compound Conditions in Selection (Multiple choice, 25 XP)

**Instructions:**
Which Cambridge pseudocode condition correctly checks if a student's age is between 11 and 18 inclusive?

**Options:**
- **A**: (Age &gt;= 11) OR (Age &lt;= 18)
- **B**: (Age &gt;= 11) AND (Age &lt;= 18)
- **C**: 11 &lt;= Age &lt;= 18
- **D**: (Age &gt;= 11) &amp;&amp; (Age &lt;= 18)


**Autotest Expectations:**
- **Correct logical AND condition chosen**: `selection_match` -> Expected: `opt_and`

### Exercise 2: Exam-Style Question: Complete Nested IF Branching (Fill in blank, 35 XP)

**Instructions:**
Complete the missing keywords in this nested selection construct:

```pseudocode
IF Temperature > 30 THEN
    Status <- "Hot"
[blank_1]
    IF Temperature < 15 THEN
        Status <- "Cold"
    ELSE
        Status <- "Moderate"
    [blank_2]
[blank_3]
```


**Starter Code:**
```text
{"template":"IF Temperature > 30 THEN\n    Status <- \"Hot\"\n[blank_1]\n    IF Temperature < 15 THEN\n        Status <- \"Cold\"\n    ELSE\n        Status <- \"Moderate\"\n    [blank_2]\n[blank_3]","word_bank":["ELSE","ELSEIF","ENDIF","THEN","FI"],"prompt":"Exam-Style Question: Complete Nested IF Branching","blanks":[{"id":"blank_1"},{"id":"blank_2"},{"id":"blank_3"}]}
```

**Autotest Expectations:**
- **Branch keyword is ELSE**: `text_exact` -> Expected: `ELSE`
- **Inner IF terminated with ENDIF**: `text_exact` -> Expected: `ENDIF`
- **Outer IF terminated with ENDIF**: `text_exact` -> Expected: `ENDIF`

### Exercise 3: Independent Practice: Grade boundaries (Pseudocode, 30 XP)

**Instructions:**
Read an integer mark from 0 to 100. Output A for at least 80, B for at least 60 but below 80, C for at least 40 but below 60, otherwise U.

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:**
- **Mark 0**: `test_case` -> Expected: `{&quot;inputs&quot;:[0],&quot;output&quot;:&quot;U&quot;}`
- **Mark 39**: `test_case` -> Expected: `{&quot;inputs&quot;:[39],&quot;output&quot;:&quot;U&quot;}`
- **Mark 40**: `test_case` -> Expected: `{&quot;inputs&quot;:[40],&quot;output&quot;:&quot;C&quot;}`
- **Mark 59**: `test_case` -> Expected: `{&quot;inputs&quot;:[59],&quot;output&quot;:&quot;C&quot;}`
- **Mark 60**: `test_case` -> Expected: `{&quot;inputs&quot;:[60],&quot;output&quot;:&quot;B&quot;}`
- **Mark 79**: `test_case` -> Expected: `{&quot;inputs&quot;:[79],&quot;output&quot;:&quot;B&quot;}`
- **Mark 80**: `test_case` -> Expected: `{&quot;inputs&quot;:[80],&quot;output&quot;:&quot;A&quot;}`
- **Mark 100**: `test_case` -> Expected: `{&quot;inputs&quot;:[100],&quot;output&quot;:&quot;A&quot;}`

