# Count-Controlled Loops (FOR ... TO ... NEXT)

- **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 algorithm design, iteration refers to repeating a block of instructions. When the exact number of iterations is known before entering the loop, developers use a **Count-Controlled Loop** (also called a `FOR` loop).

---

## 1. Cambridge Syntax

```pseudocode
FOR <Counter> <- <StartValue> TO <EndValue>
    // Statements executed on each iteration
NEXT <Counter>
```

### Automatic Loop Counter Management:

- The loop counter is initialized to `<StartValue>`.
- At each `NEXT <Counter>`, the runtime engine automatically increments `<Counter>` by 1 (or by `<StepValue>` if specified).
- When `<Counter>` exceeds `<EndValue>`, the loop terminates immediately and execution moves to the next statement.
- *Exam Rule*: Do NOT manually increment the loop counter inside the loop body (e.g. `i <- i + 1`), as this disrupts automatic step tracking!

---

## 2. Custom Steps and Decrementing

To increment by values other than 1, or to count backwards, use the `STEP` clause:

```pseudocode
// Step by 2 (Odd numbers from 1 to 9):
FOR i <- 1 TO 9 STEP 2
    OUTPUT i
NEXT i

// Counting backwards (Countdown from 10 to 1):
FOR Count <- 10 TO 1 STEP -1
    OUTPUT Count
NEXT Count
```

---

## 3. Typical Use Cases for `FOR` Loops

1. **Processing Arrays**: Iterating from index `1` to `Length(Array)`.
2. **Fixed-Size Data Sets**: Entering grades for a known class of 30 students.
3. **Mathematical Progressions**: Computing factorials, geometric progressions, or multiplication tables.

---

## Interactive Exercises & Autotest Specifications

### Exercise 1: Exam-Style Check: When to Choose a Count-Controlled Loop (Multiple choice, 25 XP)

**Instructions:**
Under which circumstance is a count-controlled `FOR ... TO ... NEXT` loop the most appropriate iterative construct?

**Options:**
- **A**: When reading user inputs until a sentinel rogue value such as -1 is entered.
- **B**: When the loop body must execute at least once before checking any condition.
- **C**: When implementing an infinite background daemon process.
- **D**: When the exact number of iterations is known before the loop begins execution.


**Autotest Expectations:**
- **Correct use case for FOR loop identified**: `selection_match` -> Expected: `opt_known`

### Exercise 2: Exam-Style Question: Complete Decrementing FOR Loop Header (Fill in blank, 35 XP)

**Instructions:**
Complete the Cambridge pseudocode header and footer to count down from `10` down to `1`:

```pseudocode
FOR Count <- 10 [blank_1] 1 [blank_2] -1
    OUTPUT Count
[blank_3] Count
```


**Starter Code:**
```text
{"template":"FOR Count <- 10 [blank_1] 1 [blank_2] -1\n    OUTPUT Count\n[blank_3] Count","word_bank":["TO","STEP","NEXT","UNTIL","DOWNTO","ENDFOR"],"prompt":"Exam-Style Question: Complete Decrementing FOR Loop Header","blanks":[{"id":"blank_1"},{"id":"blank_2"},{"id":"blank_3"}]}
```

**Autotest Expectations:**
- **Boundary keyword is TO**: `text_exact` -> Expected: `TO`
- **Step keyword is STEP**: `text_exact` -> Expected: `STEP`
- **Terminator keyword is NEXT**: `text_exact` -> Expected: `NEXT`

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

**Instructions:**
Read an integer N from 0 to 8. Output N factorial. Remember that 0 factorial is 1.

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:**
- **Empty product**: `test_case` -> Expected: `{&quot;inputs&quot;:[0],&quot;output&quot;:&quot;1&quot;}`
- **One**: `test_case` -> Expected: `{&quot;inputs&quot;:[1],&quot;output&quot;:&quot;1&quot;}`
- **Five**: `test_case` -> Expected: `{&quot;inputs&quot;:[5],&quot;output&quot;:&quot;120&quot;}`
- **Eight**: `test_case` -> Expected: `{&quot;inputs&quot;:[8],&quot;output&quot;:&quot;40320&quot;}`

