Nested Loops in C – Thinking in Layers
In the last lessons, you learned how to use loops to repeat code.
Now we are going one step deeper.
Today, we learn how to make loops work inside other loops.
This is called a nested loop.
If you understand this properly, you’ll be able to:
Print patterns
Work with grids
Process rows and columns
Build more complex logic
Let’s break it down slowly.
1. Quick Recap: What Is a Loop?
A loop allows code to run multiple times.
Example:
int i = 0;
while (i < 3) {
printf("Hello\n");
i++;
}
This prints “Hello” three times.
The loop:
Starts with
i = 0Runs while
i < 3Increases
ieach timeStops when the condition becomes false
Simple.
2. What Is a Nested Loop?
A nested loop is:
A loop inside another loop.
There are two loops:
Outer loop
Inner loop
The most important rule:
The inner loop completes all its iterations every time the outer loop runs once.
Read that again.
Every time the outer loop runs once, the inner loop runs completely from start to finish.
3. Visual Example (Think in Rows and Columns)
Imagine printing this pattern:
* * *
* * *
* * *
This is:
3 rows
3 stars in each row
So we need:
One loop to control rows
One loop to control stars inside each row
That’s nested looping.
4. Basic Structure of a Nested Loop
Example using while loops:
int i = 0;
while (i < 3) { // Outer loop
int j = 0; // Reset inner counter
while (j < 3) { // Inner loop
printf("* ");
j++;
}
printf("\n"); // Move to next line
i++;
}
Let’s understand what happens.
5. Step-by-Step Execution
Start with:
i = 0
Outer loop condition: i < 3 → true.
Now inside outer loop:
j = 0
Inner loop condition: j < 3 → true.
Inner loop runs:
Print *
j = 1
Print *
j = 2
Print *
j = 3
Stop inner loop
Now:
Print newline
i = 1
Outer loop runs again.
This continues until i = 3.
Total stars printed:
3 rows × 3 stars = 9 stars.
6. Important Rule About Resetting
Notice this line:
int j = 0;
It is inside the outer loop.
Why?
Because we must reset the inner counter every time the outer loop runs.
If you move int j = 0; outside the outer loop,
the inner loop will not behave correctly.
This is a very common beginner mistake.
7. Using for Loops Instead
Nested loops are often written using for.
Same example:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("* ");
}
printf("\n");
}
This is shorter but does exactly the same thing.
Outer loop controls rows.
Inner loop controls columns.
8. Another Example: Number Grid
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
printf("%d ", j);
}
printf("\n");
}
Output:
1 2 3
1 2 3
1 2 3
Notice:
The inner loop always completes fully before the outer loop continues.
9. Common Beginner Mistakes
Forgetting to reset inner counter
Infinite loops (missing increment)
Mixing up
iandjMisplacing braces
Always check:
Does each loop have its own counter?
Is the inner counter reset?
Is the increment inside the correct loop?
10. When Do We Use Nested Loops?
Nested loops are useful for:
Printing patterns
Working with 2D arrays (later topic)
Comparing every element with every other element
Creating multiplication tables
Processing grids
Any time you need:
“Do this many times, and inside that, do something else many times.”
11. Practice Exercises
Print 4 rows of 5 stars.
Print this pattern:
1
1 2
1 2 3
- Print a 3×3 multiplication grid.
Final Thoughts
Nested loops are about thinking in layers.
Outer loop → controls big structure
Inner loop → controls detail inside that structure
If you remember only one thing from today, remember this:
The inner loop finishes completely every time the outer loop runs once.
In the next lesson, we will learn how to organize code better using functions.
And that is where your programs start becoming structured and powerful.
About the author
- Heya! 👋 I love helping people, and one of the best ways I do this is by sharing what I learn and build. I started as a pharmacist at a tertiary hospital in Northern Ghana, where I saw how much technology could improve healthcare. That sparked my interest in digital health. I taught myself to code, then joined the ALX Software Engineering program, which became a turning point. I went on to complete a Master of Health Informatics at the University of Toronto. Today I am a software and AI engineer and a health informatician, and I practise remotely as a Digital Health Pharmacist. I combine clinical experience with technical skills to build tools that solve real problems in health and beyond, and I write here about what I build and what I learn.
Comments
Join the discussionNo comments yet. Be the first to comment.
