You already know two of the great powers of programming: selection — making a
choice with if — and iteration — repeating work with a loop. The
exciting next step is realising you can put these building blocks inside each other. An
if can live inside another if. A loop can live inside another loop. When
one control structure sits inside another, we say it is nested — the word we use
for a smaller box tucked inside a bigger one.
Think of a set of instructions like "for each pupil in the class, mark each question on their test". There are two "for each"s there, one inside the other: the outer one steps through the pupils, and for every single pupil the inner one steps through all the questions. That is a nested loop, and it captures something computers do constantly. This page is all about that one idea: things inside things, and the golden rule that comes with it — the inner thing runs fully on every pass of the outer thing.
Sometimes one choice isn't enough — after you've made the first decision, you need to make a second one within it. Imagine grading a test out of 100. First we decide pass or fail at 50. But if the pupil passed, we then want to decide how well — a merit, or a top-grade distinction. That second decision only makes sense once we already know they passed, so it belongs inside the "passed" branch. Run this:
See how the inner if is tucked neatly inside the outer one, pushed further to the right?
That indentation isn't decoration — it's how you (and every other programmer) see the
nesting at a glance. The inner check about distinctions is never even looked at for a failing
score, because we never enter the outer if at all. Change score to
Now the star of the show. When you put one loop inside another, the outer loop starts a pass, and before it can move on, the inner loop runs all the way through from start to finish. Then the outer loop takes its next step, and the inner loop runs completely again. Over and over.
The classic first example is drawing a rectangle of stars. The outer loop handles the rows; for each row, the inner loop prints the stars across, and then we move to a fresh line. Run it and count:
The outer loop ran line = line + "★ "
happened
Before more code, let's see exactly what "the inner loop runs fully on each outer pass"
looks like. Step through a small
By the end, every one of the
Nested loops get really useful when the inner body uses both counters at once. Here the outer
counter
Every number in that square is size to
if inside
Nesting isn't only loop-in-loop or if-in-if — you can freely mix them. A very common pattern is a
loop that makes a decision on every pass. Here we go through the numbers
if
decides whether to shout that it's even or odd:
The loop repeats; the if inside it chooses freshly each time. This "loop, and decide
something every pass" shape is one of the most useful in all of programming — filtering a list,
checking each item, tallying things up. You'll reach for it again and again.
There's no limit — you can nest as deep as the problem needs. A loop over years, containing a loop
over months, containing a loop over days would be three loops deep, and would run
its innermost body
Here is the single most important thing to remember about nested loops. A loop of
That multiplication is easy to underestimate. A loop of
Because nested code has things inside things, it is dangerously easy to lose track of which
loop or if you're inside. Two habits keep you safe:
if), push the code one step further to the right. The staircase shape makes the
nesting visible instantly.i, do
not reuse i for the inner loop — use j, or a meaningful name
like row and col. Reusing the same counter name for both loops is a
confusing bug that makes the loops trip over each other.Neat indentation and distinct counter names cost nothing and save you from hours of head-scratching.