CNested Loops

Nested Loops

A nested loop is simply a loop placed inside the body of another loop. For every single iteration of the outer loop, the entire inner loop runs from start to finish. Nested loops are the standard way to work with grids, tables, multi-dimensional data, and any situation that requires comparing every element against every other element.
Basic Structure

C
#include <stdio.h>

int main(void) {
    for (int row = 0; row < 3; row++) {
        for (int col = 0; col < 3; col++) {
            printf("(%d,%d) ", row, col);
        }
        printf("\n");
    }
    // Output:
    // (0,0) (0,1) (0,2)
    // (1,0) (1,1) (1,2)
    // (2,0) (2,1) (2,2)

    return 0;
}
Worked Example: A Multiplication Table

Nested loops make short work of printing a multiplication table, where the outer loop selects the row number and the inner loop walks across each column of that row.

C
#include <stdio.h>

int main(void) {
    int size = 5;

    for (int row = 1; row <= size; row++) {
        for (int col = 1; col <= size; col++) {
            printf("%4d", row * col);
        }
        printf("\n");
    }

    return 0;
}
/* Output:
   1   2   3   4   5
   2   4   6   8  10
   3   6   9  12  15
   4   8  12  16  20
   5  10  15  20  25
*/
Worked Example: A Triangle Pattern

Nested loops are also the go-to tool for printing patterns, where the inner loop's trip count depends on the outer loop's current value.

C
#include <stdio.h>

int main(void) {
    int rows = 5;

    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}
/* Output:
   *
   **
   ***
   ****
   *****
*/
Performance: Iterations Multiply
If an outer loop runs n times and the inner loop runs m times for each outer iteration, the body executes n × m total times, not n + m. When both loops scale with the same input size n, the total work grows as — commonly written O(n²). This is fine for small or moderate inputs but can become a real bottleneck as n grows large, so it is worth noticing whenever you nest loops over the same dataset.

C
// O(n^2): for n = 1000, the body runs 1,000,000 times
for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        // compare arr[i] and arr[j], for example
    }
}
Tip
Whenever you catch yourself nesting a loop over the same array inside another loop over that array, ask whether a smarter algorithm (e.g. a hash table lookup, or sorting first) could avoid the quadratic blow-up for large inputs.
break Only Exits the Innermost Loop
A break statement inside a nested loop only terminates the loop it is directly inside — the innermost one. It does not stop any outer loop, which surprises programmers coming from languages with labeled breaks.

C
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        if (j == 1) {
            break; // exits only the inner (j) loop
        }
        printf("i=%d j=%d\n", i, j);
    }
    // the outer (i) loop continues normally
}
// Output: i=0 j=0 / i=1 j=0 / i=2 j=0
Warning
C has no labeled break like some other languages do. Exiting multiple nested loops directly with a single statement requires either a flag variable checked at each level, wrapping the loops in a function and using return, or the goto statement — the one commonly accepted, idiomatic use of goto in real C code. See the dedicated goto page for that pattern in detail.
Key Points
  • A nested loop runs its inner loop fully for every single iteration of the outer loop.

  • Total iterations multiply: an outer loop of n and an inner loop of m run the body n * m times.

  • When both loop bounds scale with input size, total work grows as O(n^2) -- watch for this on large inputs.

  • break only exits the innermost loop it appears in; it never terminates an outer loop.

  • Exiting multiple nested loops at once typically needs a flag variable, an early return, or goto.