A Loop Inside a Loop
Loops within loops.
A Loop Inside a Loop is a free Coding for Kids lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Coding for Kids learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Loops Are Awesome! 🔁
Hi superstar! 🌟 You already know a loop repeats things. Today we learn something SUPER cool: a loop inside another loop!
It's like a robot doing a little dance over and over, while a bigger robot makes it dance again and again! 🤖
for i in range(3):
print('Outer loop! 🤖')What Is Nesting? 🪺
When you put one loop inside another, we call it a nested loop. Just like a tiny bird's nest sitting inside a big tree! 🪺🌳
The inside loop finishes ALL its turns before the outside loop moves on.
for big in range(2):
for small in range(2):
print('hello!')Counting With Two Loops 🔢
Let's watch the numbers! The outer loop picks a number, then the inner loop runs all the way through before going back up.
- Outer = the big steps
- Inner = the little steps
for a in range(2):
for b in range(3):
print('a =', a, 'b =', b)Indenting Matters! ➡️
In Python, the spaces in front of code tell it where it belongs. The inner loop's code is pushed in even MORE! ➡️➡️
Think of it like stairs going down. Each loop is one more step in!
for x in range(2):
print('Step in once')
for y in range(2):
print(' Step in twice!')Stars and Stars ⭐
Let's make rows of stars! The outer loop makes a new row, and the inner loop prints stars on that row. ⭐⭐⭐
We use end='' so stars stay on the same line!
for row in range(3):
for star in range(4):
print('*', end='')
print()Robot Dance Party 🕺
Imagine 3 robots. Each robot spins around 2 times. How many spins is that all together? Let's let the computer count for us!
for robot in range(3):
print('Robot', robot + 1, 'is dancing!')
for spin in range(2):
print(' spin!')Inner Finishes First 🏁
Here's the secret rule: the inner loop runs completely for every single turn of the outer loop. 🏁
So if outer runs 2 times and inner runs 3 times, that's 2 × 3 = 6 total prints!
count = 0
for a in range(2):
for b in range(3):
count = count + 1
print('Total runs:', count)Naming Your Loops 🏷️
You can name your loop helpers anything! People often use i and j as short names. ✏️
But row and col are even friendlier names. Pick whatever helps you understand! 😊
for i in range(2):
for j in range(2):
print('i and j:', i, j)Make a Box 📦
With two loops we can draw a little box of hashes! The outer loop is the number of rows, the inner loop fills each row. 📦
for row in range(3):
for col in range(5):
print('#', end='')
print()Loops Inside Loops Inside... 🌀
You can even put a loop inside a loop inside a loop! But let's keep it to two for now. Three is super dizzy! 🌀
Most cool patterns only need TWO loops. Nice and simple!
for a in range(2):
for b in range(2):
print('Pair:', a, b)Your Turn to Imagine ✨
Try changing the numbers in range() and see what happens! Bigger numbers = more rows and more stars. ✨
Playing and changing things is how the best coders learn! 🚀
for row in range(4):
for star in range(6):
print('+', end='')
print()Quick Check! 🧠
Time for a fun question! Think about how nested loops count. 🤔
You Did It! 🎉
Wow, you learned nested loops! 🎉
- A loop inside a loop is called nested 🪺
- The inner loop finishes before the outer moves on 🏁
- Indenting (spaces) tells loops where they belong ➡️
- Multiply the turns to count total runs 🔢
Next up: drawing cool grids! 🟦
for row in range(2):
for col in range(3):
print('🌟', end='')
print()Frequently asked questions
Is the “A Loop Inside a Loop” lesson free?
Yes — the full text of “A Loop Inside a Loop” is free to read here on the web, and the Coding for Kids course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Coding for Kids course, upgrade to CoddyKit PRO.
What will I learn in “A Loop Inside a Loop”?
Loops within loops. You practise Coding for Kids with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Coding for Kids?
No prior experience is required. Coding for Kids on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “A Loop Inside a Loop” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Coding for Kids lesson?
Yes. Every Coding for Kids lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- A Loop Inside a Loop
- Drawing a Grid
- Star Triangles
- Multiplication Table