Nested Loops Inside Loops
A nested loop is a loop that runs completely inside another loop, like a clock's minute hand spinning all the way around every time the hour hand moves one step.
Reading is good — doing is better. Practice Nested Loops Inside Loops as an interactive lesson.
Try the lessonDefinition
A nested loop means putting one loop inside another loop. The outer loop runs a set number of times, and every single time it does, the inner loop runs all the way through from start to finish. Together they let a computer repeat a pattern many times without you having to write the same instructions over and over.
Remember the rule
Total repetitions = Outer Loop times × Inner Loop times. If the outer loop runs 3 times and the inner loop runs 4 times, the inner action happens 3 × 4 = 12 times total.
Key words
- Loop
- A set of instructions the computer repeats a certain number of times before moving on.
- Nested Loop
- A loop that lives inside another loop, so the inside one runs completely each time the outside one takes one step.
- Outer Loop
- The first, bigger loop that controls how many rounds the whole process goes through.
- Inner Loop
- The loop tucked inside the outer loop that runs all the way through on every single round.
- Iteration
- One single trip through a loop — like one spin of a wheel.
- Repeat
- A block command that tells the computer to do something a chosen number of times.
- Counter
- A number that keeps track of how many times a loop has run so far.
- Pattern
- Something that repeats in a predictable, orderly way — nested loops are great for making patterns.
Worked examples
The outer loop runs 2 times. The inner loop runs 3 times and prints a star each time. How many stars are printed?
→ 6 stars. Round 1 of the outer loop → inner loop prints ★ ★ ★. Round 2 of the outer loop → inner loop prints ★ ★ ★. Total: 2 × 3 = 6 stars. · The inner loop always resets to the beginning each time the outer loop takes a new step.
A robot is told: outer loop repeat 3 times, inner loop repeat 2 times move forward. How many times does the robot move forward?
→ 6 times. Each of the 3 outer rounds makes the robot move forward twice: 3 × 2 = 6 moves total. · Think of the outer loop as the rows and the inner loop as the steps inside each row.
You want to draw a grid of dots that is 4 columns wide and 3 rows tall. Which loop controls rows and which controls columns?
→ The outer loop runs 3 times for the 3 rows. The inner loop runs 4 times for the 4 dots in each row. Total dots drawn: 3 × 4 = 12 dots. · Grids are the most common real-world use of nested loops.
A music app plays 2 songs, and for each song it plays the chorus 4 times. How many times does the chorus play in all?
→ 8 times. Outer loop: 2 songs. Inner loop: 4 chorus plays per song. 2 × 4 = 8 chorus plays total. · Nested loops show up wherever you repeat something inside something else that is already repeating.
In Scratch, an outer repeat block says 'repeat 5' and inside it is a 'repeat 3' block that plays a drum beat. How many drum beats happen?
→ 15 drum beats. 5 outer rounds × 3 beats each round = 15 beats total.
Common mistakes
- Forgetting that the inner loop restarts from the beginning every time the outer loop moves to its next step — it does not continue from where it stopped.
- Multiplying wrong: children sometimes add instead of multiply (3 outer + 4 inner = 7 instead of the correct 3 × 4 = 12).
- Mixing up which loop is the outer and which is the inner, causing the pattern to come out backwards or scrambled.
- Leaving out the inner loop's end or close bracket in code, which makes the computer think everything after it is still inside the loop.
- Making both loops run forever by accident, which freezes the program — always give each loop a definite stopping number.
FAQs
Why do we need a loop inside a loop? Can't we just use one big loop?
One loop can only repeat one thing a flat number of times. A nested loop lets you repeat a whole pattern — not just one step — over and over. For example, one loop alone cannot draw a grid; you need a loop for columns running inside a loop for rows.
Does the inner loop always have to have the same number of steps as the outer loop?
No! The outer and inner loops can each have any number of steps. An outer loop of 5 and an inner loop of 2 is perfectly fine. They do not have to match.
How do I figure out the total number of times something happens with nested loops?
Multiply the outer loop's number by the inner loop's number. Outer = 4, Inner = 6 → 4 × 6 = 24 total times the inner action happens.
What happens when the outer loop finishes one round?
The inner loop resets completely back to step one and runs all the way through again for the next round of the outer loop.
Can you have more than two loops nested together?
Yes, you can put a third loop inside the second loop, but that gets complicated. In 4th grade we mostly work with just two loops nested together.
Where do we see nested loops in real life, not just in coding?
A calendar is a great example: the outer loop goes through 12 months, and the inner loop counts each day inside that month. A carton of eggs is another: 2 rows (outer) with 6 eggs each (inner) = 12 eggs total.
Want the whole picture for your child?
Every K–6 subject, an AI tutor that teaches step by step, unlimited practice, and a reward world.
Start a 3-day free trial