A nested loop is a loop placed inside the body of another loop. For every single pass of the outer loop, the inner loop runs from start to finish. You can nest for, while, and do-while in any combination.
Remember
for (outer init; outer test; outer update) {
for (inner init; inner test; inner update) {
/* inner body */
}
/* runs once per outer pass, after the inner loop ends */
}
total inner-body runs = outer passes x inner passes
Layer
Runs
Typical job
Outer loop
Once per row / group
Pick the row, line, or group
Inner loop
Fully, each outer pass
Walk the columns or items in that row
Code after inner loop
Once per outer pass
Print a newline, reset totals
Approach
How to Use Nested Loops in C
Give each loop its own counter, restart the inner counter every outer pass, and decide where the newline or subtotal belongs.
Choose the outer counter (for example r for rows) and the inner counter (c for columns).
Declare the inner counter inside the outer body so it restarts at its initial value each time.
Put per-item work (like printf("* ")) in the inner body.
Put per-row work (like printf("\n")) after the inner loop, still inside the outer body.
Compile with gcc file.c -std=c11 -o out and run.
Piece
Required?
Role
Outer loop header
Yes
Controls how many times the inner loop restarts
Inner loop header
Yes
Controls work inside one outer pass
Separate counters
Yes
Reusing one variable breaks both loops
Statement between loops
Optional
Newlines, subtotals, resets
Pseudocode
Pseudocode
for each row from 0 to rows - 1:
for each column from 0 to cols - 1:
do the per-cell work
do the per-row work (newline, subtotal)
// inner loop restarts from its first value for every row
Reference
Cheat Sheet
Keep these facts handy when you write loops inside loops.
Topic
Detail
Total iterations
outer x inner (if the inner bound is constant)
Triangular inner bound
for (c = 0; c <= r; c++) gives about n(n+1)/2 runs
Inner counter
Reset automatically when declared in the inner for header
Trace for (r = 0; r < 2; r++) { for (c = 0; c < 3; c++) printf("*"); printf("\n"); } one step at a time.
Calculation
r = 0
c = 0 -> print *
c = 1 -> print *
c = 2 -> print *
c = 3 -> test fails, inner loop ends
print newline row 1: ***
r = 1
c = 0 -> print *
c = 1 -> print *
c = 2 -> print *
c = 3 -> test fails, inner loop ends
print newline row 2: ***
r = 2 -> test fails, outer loop ends
inner body ran 2 x 3 = 6 times
Code
C Examples
Compile with gcc file.c -std=c11 -o out. Compare with the single-loop forms in for and while.
Example 1 — Print a 3 x 4 Grid of Stars
The classic first nested loop: rows on the outside, columns on the inside.
C
#include <stdio.h>
int main(void) {
int rows = 3;
int cols = 4;
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Output
* * * *
* * * *
* * * *
How It Works
Outer loop picks the row.r goes 0, 1, 2.
Inner loop fills the row.c restarts at 0 and prints four stars.
Newline after the inner loop. It runs once per row, not once per star.
Total. 3 x 4 = 12 stars are printed.
Example 2 — Print a 3 x 3 Multiplication Grid
Both counters take part in the calculation, so each cell is i * j.
C
#include <stdio.h>
int main(void) {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
printf("%4d", i * j);
}
printf("\n");
}
return 0;
}
Output
1 2 3
2 4 6
3 6 9
How It Works
Row number is i. It stays fixed while the inner loop runs.
Column number is j. It cycles 1 to 3 for every row.
Width helps alignment.%4d pads each product to four characters.
Newline per row. Placed after the inner loop.
Example 3 — break Leaves Only the Inner Loop
A break inside a nested loop stops the innermost loop only; the outer loop keeps going.
C
#include <stdio.h>
int main(void) {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
break;
}
printf("(%d,%d) ", i, j);
}
printf("\n");
}
return 0;
}
Outer loop continues. The newline prints, then i increases.
Takeaway. To leave both loops, use a flag or return.
Edge Cases & Pitfalls
Check these before shipping loops inside loops.
counter
Reusing the same variable
If both loops use i, the inner loop changes the outer counter and the loops misbehave or never end. Use r/c or i/j.
reset
Forgetting to reset the inner counter
With a while inner loop, set c = 0 inside the outer body. Declared once outside, c stays at its last value and later rows print nothing.
placement
Newline in the wrong spot
Put printf("\n") after the inner loop. Inside it, every cell gets its own line.
exit
break only leaves one loop
Use a flag checked by the outer loop, a goto with care, or move the loops into a function and return.
cost
Quadratic growth
Doubling both bounds quadruples the work. Three levels grow cubically, so avoid deep nesting on large inputs.
Analysis
Performance Notes
Shape
Iterations
Typical use
Two loops, bounds n and m
n x m
Grids, matrices, tables
Inner bound grows with outer
About n(n+1)/2
Triangles, pairs (i, j) with j <= i
Three nested loops
n x m x k
3D data, matrix multiply
Nested loops multiply iteration counts, so the innermost body dominates run time. Keep it small and move work that does not depend on the inner counter out of the inner loop. See for for counted loop basics.
Remember
Key Takeaways
Role: a loop inside a loop; the inner one runs fully for each outer pass.
Count: total body runs equal outer passes times inner passes.
Counters: give each loop its own variable and restart the inner one each row.
Exit:break and continue affect only the innermost loop.
One line: outer loop picks the row, inner loop walks the columns.
Frequently Asked Questions
A nested loop is a loop inside another loop's body. The inner loop completes all its iterations for each single iteration of the outer loop.
Once per outer iteration, so the inner body runs outer x inner times in total when the inner bound is constant.
Yes. Any loop type can be nested in any other. Pick the form that matches each level's role (counted, condition-based, or at-least-once).
No. break leaves only the innermost loop containing it. Use a flag, return from a function, or goto to leave several levels.
A for loop resets it through its init clause. With while loops you must set the counter to its starting value inside the outer body, or later rows will skip the inner loop.
Make the inner bound depend on the outer counter, for example for (c = 0; c <= r; c++). Row r then prints r + 1 items.