# 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:

```c
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:

```plaintext
* * *
* * *
* * *
```

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:

```c
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:

```plaintext
i = 0
```

Outer loop condition: `i < 3` → true.

Now inside outer loop:

```plaintext
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:

```c
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:

```c
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

```c
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        printf("%d ", j);
    }
    printf("\n");
}
```

Output:

```plaintext
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:
    

```plaintext
1
1 2
1 2 3
```

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.
