Nested Loops in C (original) (raw)

Last Updated : 7 Nov, 2025

A nested loop means a loop statement inside another loop statement.

1. Nested for Loops

Nested for loop refers to any type of loop that is defined inside a 'for' loop.

C `

#include <stdio.h>

int main() {

// limit for i
int m = 4;  

// limit for j
int n = 5;  

// Outer loop
for (int i = 0; i < m; i++) {
    printf("i = %d: ", i);
    
    // InnerLoop
    for (int j = 0; j < n; j++) {
        printf("%d ", i * n + j);
    }
    printf("\n");
}

return 0;

}

`

Output

i = 0: 0 1 2 3 4 i = 1: 5 6 7 8 9 i = 2: 10 11 12 13 14 i = 3: 15 16 17 18 19

Below is the equivalent flow diagram for nested 'for' loops:

Nested-for-Loop

**2. Nested while Loops

A nested while loop refers to any type of loop that is defined inside a 'while' loop.

C `

#include <stdio.h>

int main() { int end = 5;

printf("Pattern Printing using Nested While loop");

int i = 1;

while (i <= end) {
    printf("\n");
    int j = 1;
    while (j <= i) {
        printf("%d ", j);
        j = j + 1;
    }
    i = i + 1;
}
return 0;

}

`

Output

Pattern Printing using Nested While loop 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5

Below is the equivalent flow diagram for nested 'while' loops:

Nested-while-Loop

3. Nested do-while Loops

A nested do-while loop refers to any type of loop that is defined inside a do-while loop.

C `

#include <stdio.h>

int main() { int i = 1;

do
{
    int j = 1;
    do
    {
        printf("%d ", j);
        j++;
    } while (j <= 3);

    printf("\n");
    i++;
} while (i <= 2);

return 0;

}

`

Below is the equivalent flow diagram for nested 'do-while' loops:

Nested-do-while-Loop

4. Hybrid Nested Loops

A hybrid loop structure is where any type of loop is nested inside another loop type.

C++ `

#include <stdio.h>

int main() { int i = 1; while (i <= 2) { // Outer while loop for (int j = 1; j <= 2; j++) { // Inner for loop printf("i = %d, j = %d\n", i, j); } i++; } return 0; }

`

Output

i = 1, j = 1 i = 1, j = 2 i = 2, j = 1 i = 2, j = 2

Break Inside Nested Loops

Whenever we use a break statement inside the nested loops it breaks the innermost loop only and program control goes to the outer loop. Breaking the inner loop does not affects the outer loops.

C `

#include <stdio.h>

int main() {

int i = 0;
for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 3; j++) {
        
        // This inner loop will break when i==3
        if (i == 3) {
            break;
        }
        printf("* ");
    }
    printf("\n");
}
return 0;

}

`

Output





In the above program, the first loop will iterate from 0 to 5 but here if i will be equal to 3 it will break and will not print the * as shown in the output.

Continue Inside Nested Loops

Whenever we use a continue statement inside the nested loops it skips the iteration of the innermost loop only. The outer loop remains unaffected.

C `

#include <stdio.h>

int main() {

int i = 0;
for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) {
        
        // This inner loop will skip when j==2
        if (j==2) {
            continue;
        }
        printf("%d ",j);
    }
    printf("\n");
}
return 0;

}

`

In the above program, the inner loop will be skipped when j will be equal to 2. The outer loop will remain unaffected.

Uses of Nested Loops