Nested loops handle rows and columns at once.
A loop inside a loop is perfect for grids, tables, and repeated patterns.
A nested loop is a loop placed inside another loop. The outer loop runs the whole inner loop on each of its passes. This structure naturally produces grid and table patterns, where the outer loop handles rows and the inner loop handles columns.
This topic is part of Big Idea 3: Algorithms and Programming. You will learn how the inner loop restarts each outer pass and how to count total iterations.
Why this matters: Nested loops appear whenever a program processes a grid or repeats a pattern in rows, a common exam scenario.
The inner loop finishes fully for every outer pass.
Multiply the counts to find total iterations.
When loops are nested, the inner loop completes all of its iterations each time the outer loop runs once. If the outer loop runs 3 times and the inner loop runs 4 times, the inner body runs 3 × 4 = 12 times in total.
This is why nested loops map cleanly onto grids. Think of the outer loop choosing a row, then the inner loop filling every column in that row before the outer loop moves to the next row.
Where you place a statement matters. Code in the inner loop runs on every inner pass; code in the outer loop (but outside the inner loop) runs once per row. Tracing the output carefully shows the pattern.
A 3-row by 4-column grid: the outer loop sets the row, the inner loop fills the columns.
Read the grid row by row. Each row is one pass of the outer loop; the four stars in a row are the four passes of the inner loop. Multiply 3 rows by 4 columns to get 12 inner iterations. On the exam, count total runs by multiplying the loop counts.
Talking about loops within loops.
These terms keep tracing organized.
- Outer loop: the loop that contains another loop.
- Inner loop: the loop placed inside the outer loop.
- Nested loop: a loop inside another loop.
- Total iterations: outer count multiplied by inner count.
- Grid pattern: rows and columns produced by nesting.
- Loop trace: following both loops to predict output.
Memory hook: Outer = rows, inner = columns. The inner loop restarts for every new row.
How nested loops are tested.
Counting iterations and tracing output.
Be ready to:
- Count total inner iterations by multiplying the loop counts.
- Trace the output of a nested loop, row by row.
- Recognize that the inner loop restarts each outer pass.
- Tell whether a statement belongs in the inner or outer loop.
Questions often ask "How many times does this run?" or show a pattern of output. Both require understanding that the loops multiply.
Exam tip: Total inner runs equal outer count times inner count. For 3 and 4, that is 12, not 7.
Display a 3 by 4 grid of stars.
Build each row with the inner loop.
This nested loop displays three rows, each with four stars:
REPEAT 3 TIMES
{
rowText ← ""
REPEAT 4 TIMES
{
rowText ← rowText + "*"
}
DISPLAY(rowText)
}
Trace it. The outer loop runs 3 times, once per row. Each outer pass first resets rowText to an empty string, then the inner loop adds "*" four times, building "****". After the inner loop finishes, DISPLAY shows that row.
The output is three lines of four stars each:
****
****
****
The inner loop runs 4 times for each of the 3 outer passes, so the inner body runs 3 × 4 = 12 times in total. Notice DISPLAY sits in the outer loop, so it runs once per row, not once per star.
Nested loop pitfalls.
These produce the wrong count or pattern.
- Thinking both loops run the same total times. The inner runs outer × inner times overall.
- Forgetting the inner loop restarts. It begins again on every outer pass.
- Miscounting total iterations. Multiply the counts; do not add them.
- Placing DISPLAY in the wrong loop. Its location decides per-row versus per-item output.
- Using unnecessary nesting. Do not nest loops when a single loop would do.
Reframe: Picture filling a grid one full row at a time. The inner loop completes a row before the outer loop starts the next.
Nested loop terms at a glance.
Keep outer and inner straight.
| Term | What it is | In the example |
|---|---|---|
| Outer loop | The containing loop | REPEAT 3 TIMES |
| Inner loop | The loop inside | REPEAT 4 TIMES |
| Nested loop | A loop within a loop | The whole structure |
| Total iterations | Outer count × inner count | 3 × 4 = 12 |
| Grid pattern | Rows and columns | 3 rows of 4 stars |
| Loop trace | Following both loops | Row-by-row output |
Trace the inner loop fully, then move on.
Complete one row before starting the next.
When tracing nested loops, finish all inner iterations for the first outer pass before moving to the second. Track both counters in a small table. This keeps the row-by-row pattern clear and prevents miscounting total iterations on the exam.
Try it: If an outer loop runs 5 times and an inner loop runs 2 times, how many times does the inner body run in total?
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSP Big Idea 3 Topic 6: Nested Loops — Set 1
AP-style topic practice assessment
AP CSP Big Idea 3 Topic 6: Nested Loops — Set 2
AP-style topic practice assessment
AP CSP Big Idea 3 Topic 6: Nested Loops — Set 3
AP-style topic practice assessment