# Pre-Condition Loops (WHILE ... DO ... ENDWHILE)

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

A **Pre-Condition Loop** (the `WHILE` loop) evaluates its entry condition **before** executing the body of the loop.

---

## 1. Syntax of `WHILE ... DO ... ENDWHILE`

```pseudocode
WHILE <Condition> DO
    // Statements executed while Condition remains TRUE
ENDWHILE
```

### Critical Operational Characteristics:

1. **Zero Iterations Possible**: If `<Condition>` is initially `FALSE`, the loop body is bypassed entirely and runs zero times.
2. **Continuation Condition**: The loop continues executing **as long as the condition evaluates to `TRUE`**. It terminates the instant the condition becomes `FALSE`.
3. **Updating the Condition**: Statements inside the loop body **must modify** at least one variable present in `<Condition>`, otherwise the loop will never terminate, resulting in an **infinite loop**.

---

## 2. Choosing Between the Three Loop Types

  
| Question to Ask | Appropriate Loop Construct | Cambridge Syntax |
| --- | --- | --- |
| Is the exact number of iterations known in advance? | **Count-Controlled Loop** | `FOR i <- 1 TO N ... NEXT i` |
| Must the loop execute at least once (e.g. user input)? | **Post-Condition Loop** | `REPEAT ... UNTIL <Condition>` |
| May the loop need to execute zero times (e.g. searching)? | **Pre-Condition Loop** | `WHILE <Condition> DO ... ENDWHILE` |

---

## Interactive Exercises & Autotest Specifications

### Exercise 1: Exam-Style Check: Pre-Condition Loop Execution Guarantee (Multiple choice, 25 XP)

**Instructions:**
If the condition of a `WHILE ... DO ... ENDWHILE` loop evaluates to `FALSE` before the loop begins, how many times will the loop body execute?

**Options:**
- **A**: 1 time
- **B**: 0 times (the body is completely skipped)
- **C**: The program crashes with a syntax error
- **D**: Infinitely


**Autotest Expectations:**
- **Zero execution times identified**: `selection_match` -> Expected: `opt_zero`

### Exercise 2: Exam-Style Question: Complete the Pre-Condition WHILE Loop (Fill in blank, 35 XP)

**Instructions:**
Complete the missing keywords in this Cambridge pre-condition loop that halves a number until it is less than or equal to 1:

```pseudocode
[blank_1] Value > 1 [blank_2]
    Value <- Value / 2
[blank_3]
```


**Starter Code:**
```text
{"template":"[blank_1] Value > 1 [blank_2]\n    Value <- Value / 2\n[blank_3]","word_bank":["WHILE","DO","ENDWHILE","REPEAT","UNTIL","THEN"],"prompt":"Exam-Style Question: Complete the Pre-Condition WHILE Loop","blanks":[{"id":"blank_1"},{"id":"blank_2"},{"id":"blank_3"}]}
```

**Autotest Expectations:**
- **Header keyword is WHILE**: `text_exact` -> Expected: `WHILE`
- **Condition keyword is DO**: `text_exact` -> Expected: `DO`
- **Terminator is ENDWHILE**: `text_exact` -> Expected: `ENDWHILE`

### Exercise 3: Independent Practice: Bounded powers (Pseudocode, 30 XP)

**Instructions:**
Read a non-negative integer limit. Output powers of two starting at 1, each on a separate line, while the value is no greater than the limit. Output nothing when the limit is zero.

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:**
- **Zero iterations**: `test_case` -> Expected: `{&quot;inputs&quot;:[0],&quot;output&quot;:&quot;&quot;}`
- **Exact power**: `test_case` -> Expected: `{&quot;inputs&quot;:[8],&quot;output&quot;:&quot;1\n2\n4\n8&quot;}`
- **Between powers**: `test_case` -> Expected: `{&quot;inputs&quot;:[5],&quot;output&quot;:&quot;1\n2\n4&quot;}`

