# Post-Condition Loops (REPEAT ... UNTIL)

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

In computer programming, when an action must occur before a condition can be tested (such as prompting a user for input), developers use a **Post-Condition Loop** (`REPEAT ... UNTIL`).

---

## 1. Syntax of `REPEAT ... UNTIL`

```pseudocode
REPEAT
    // Statements executed at least once
UNTIL <TerminationCondition>
```

### The Two Critical Behaviors:

1. **Guaranteed Execution**: Because the condition is located at the bottom of the structure, the loop body is **guaranteed to run at least once**.
2. **Termination Logic**: The loop continues executing **as long as the condition is `FALSE`**. The moment the condition evaluates to **`TRUE`**, the loop terminates immediately.

---

## 2. Crucial Comparison: `REPEAT ... UNTIL` vs `WHILE ... DO`

  
| Feature | `REPEAT ... UNTIL` (Post-Condition) | `WHILE ... DO` (Pre-Condition) |
| --- | --- | --- |
| **Condition Location** | Evaluated at the **bottom** after loop body. | Evaluated at the **top** before loop body. |
| **Minimum Iterations** | **1** (Always runs at least once). | **0** (May never run if condition is initially false). |
| **Exit Condition** | Loops while `FALSE`; exits when `TRUE`. | Loops while `TRUE`; exits when `FALSE`. |
| **Typical Use Case** | User input validation, menu prompts, retry limits. | Searching arrays, reading file data until EOF. |

---

## Interactive Exercises & Autotest Specifications

### Exercise 1: Exam-Style Check: Guaranteed Minimum Iteration in Post-Condition Loops (Multiple choice, 25 XP)

**Instructions:**
How many times is the body of a `REPEAT ... UNTIL` loop guaranteed to execute under all possible circumstances?

**Options:**
- **A**: At least once (1 or more times)
- **B**: Zero or more times
- **C**: It will always loop infinitely
- **D**: At least twice


**Autotest Expectations:**
- **Minimum execution count identified**: `selection_match` -> Expected: `opt_once`

### Exercise 2: Exam-Style Question: Complete Post-Condition Validation Loop (Fill in blank, 35 XP)

**Instructions:**
Complete the keywords in this post-condition input loop ensuring a positive number greater than 0 is entered:

```pseudocode
[blank_1]
    OUTPUT "Enter positive number: "
    INPUT Number
[blank_2] Number [blank_3] 0
```


**Starter Code:**
```text
{"template":"[blank_1]\n    OUTPUT \"Enter positive number: \"\n    INPUT Number\n[blank_2] Number [blank_3] 0","word_bank":["REPEAT","UNTIL","WHILE",">","<",">=","="],"prompt":"Exam-Style Question: Complete Post-Condition Validation Loop","blanks":[{"id":"blank_1"},{"id":"blank_2"},{"id":"blank_3"}]}
```

**Autotest Expectations:**
- **Start keyword is REPEAT**: `text_exact` -> Expected: `REPEAT`
- **Terminating keyword is UNTIL**: `text_exact` -> Expected: `UNTIL`
- **Greater than operator**: `text_exact` -> Expected: `&gt;`

### Exercise 3: Independent Practice: Limited attempts (Pseudocode, 30 XP)

**Instructions:**
Allow at most three integer PIN attempts. The correct PIN is 2468. Stop immediately on success and output ACCESS; otherwise output DENIED after three wrong attempts.

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:**
- **Immediate success**: `test_case` -> Expected: `{&quot;inputs&quot;:[2468],&quot;output&quot;:&quot;ACCESS&quot;}`
- **Third attempt**: `test_case` -> Expected: `{&quot;inputs&quot;:[1111,2222,2468],&quot;output&quot;:&quot;ACCESS&quot;}`
- **Three failures**: `test_case` -> Expected: `{&quot;inputs&quot;:[1111,2222,3333],&quot;output&quot;:&quot;DENIED&quot;}`

