# Nested Loops & Multidimensional Iteration

- **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 Nested Loop is a control structure where one loop resides completely inside the body of another loop.

1. Mechanics of Nested Loops
In a nested loop:
The outer loop begins its first iteration.The inner loop executes and completes all of its iterations.Only after the inner loop finishes completely does the outer loop advance to its second iteration.This cycle repeats until the outer loop reaches its termination bound.
[Outer Iteration 1]
    ├── [Inner 1]
    ├── [Inner 2]
    └── [Inner 3]
[Outer Iteration 2]
    ├── [Inner 1]
    ├── [Inner 2]
    └── [Inner 3]
Total Inner Body Executions:
$$\text{Total Cycles} = (\text{Outer Iterations}) \times (\text{Inner Iterations})$$

2. Rules for Nested Loops
Independent Identifiers: The outer loop and inner loop must use different counter variables (e.g. Row and Col, or i and j). Re-using the same counter corrupts loop progression.Proper Nesting Order: The inner loop must be completely closed (NEXT Inner) before the outer loop closes (NEXT Outer). Crossing loop boundaries is a syntax violation.

---

## Interactive Exercises & Autotest Specifications

### Exercise 1: Exam-Style Check: Calculate Total Inner Loop Iterations (Multiple choice, 25 XP)

**Instructions:**
Consider this nested loop structure:

```pseudocode
FOR Outer <- 1 TO 4
    FOR Inner <- 1 TO 5
        OUTPUT "*"
    NEXT Inner
NEXT Outer
```

How many total asterisks (`"*"`) will be printed when this algorithm completes?

**Options:**
- **A**: 9
- **B**: 5
- **C**: 20
- **D**: 16


**Autotest Expectations:**
- **Correct iteration count calculated**: `selection_match` -> Expected: `opt_20`

### Exercise 2: Exam-Style Question: Complete Nested Loop Counter Closing (Fill in blank, 35 XP)

**Instructions:**
Complete the closing `NEXT` statements for this nested grid traversal:

```pseudocode
FOR Row <- 1 TO 3
    FOR Col <- 1 TO 4
        OUTPUT Grid[Row, Col]
    NEXT [blank_1]
NEXT [blank_2]
```


**Starter Code:**
```text
{"template":"FOR Row <- 1 TO 3\n    FOR Col <- 1 TO 4\n        OUTPUT Grid[Row, Col]\n    NEXT [blank_1]\nNEXT [blank_2]","word_bank":["Col","Row","i","j","Total","Matrix"],"prompt":"Exam-Style Question: Complete Nested Loop Counter Closing","blanks":[{"id":"blank_1"},{"id":"blank_2"}]}
```

**Autotest Expectations:**
- **Inner loop closes Col**: `text_exact` -> Expected: `Col`
- **Outer loop closes Row**: `text_exact` -> Expected: `Row`

### Exercise 3: Independent Practice: A multiplication grid (Pseudocode, 30 XP)

**Instructions:**
Read a row count and column count, both from 1 to 4. Output each row-by-column product in row order, one number per line.

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:**
- **Two by three**: `test_case` -> Expected: `{&quot;inputs&quot;:[2,3],&quot;output&quot;:&quot;1\n2\n3\n2\n4\n6&quot;}`
- **One by four**: `test_case` -> Expected: `{&quot;inputs&quot;:[1,4],&quot;output&quot;:&quot;1\n2\n3\n4&quot;}`
- **Three by one**: `test_case` -> Expected: `{&quot;inputs&quot;:[3,1],&quot;output&quot;:&quot;1\n2\n3&quot;}`

