Skip to main content

Command Palette

Search for a command to run...

Nested Loops in C – Thinking in Layers

Updated
4 min read
D

Heya! 👋 I love helping people, and one of the best ways I do this is by sharing my knowledge and experiences. My journey reflects the power of growth and transformation, and I’m here to document and share it with you.

I started as a pharmacist, practicing at a tertiary hospital in the Northern Region of Ghana. There, I saw firsthand the challenges in healthcare delivery and became fascinated by how technology could offer solutions. This sparked my interest in digital health, a field I believe holds the key to revolutionizing healthcare.

Determined to contribute, I taught myself programming, mastering tools like HTML, CSS, JavaScript, React, PHP, and more. But I craved deeper knowledge and practical experience. That’s when I joined the ALX Software Engineering program, which became a turning point. Spending over 70 hours a week learning, coding, and collaborating, I transitioned fully into tech.

Today, I am a Software Engineer and Digital Health Solutions Architect, building and contributing to innovative digital health solutions. I combine my healthcare expertise with technical skills to create impactful tools that solve real-world problems in health delivery.

Imposter syndrome has been part of my journey, but I’ve learned to embrace it as a sign of growth. Livestreaming my learning process, receiving feedback, and building in public have been crucial in overcoming self-doubt. Each experience has strengthened my belief in showing up, staying consistent, and growing through challenges.

Through this platform, I document my lessons, challenges, and successes to inspire and guide others—whether you’re transitioning careers, exploring digital health, or diving into software development.

I believe in accountability and the value of shared growth. Your feedback keeps me grounded and motivated to continue this journey. Let’s connect, learn, and grow together! 🚀

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 = 0

  • Runs while i < 3

  • Increases i each time

  • Stops 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

  1. Forgetting to reset inner counter

  2. Infinite loops (missing increment)

  3. Mixing up i and j

  4. Misplacing 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

  1. Print 4 rows of 5 stars.

  2. Print this pattern:

1
1 2
1 2 3
  1. 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.

More from this blog

D

Dr. Ehoneah Obed

78 posts

Software engineer writing about systems: in code, in learning, in life. I reverse-engineer complex problems into frameworks. Pharmacist → SWE → Founder.