← Back to C Course | Chapter 4: Control Flow | Lesson 10 of 11

C Nested for Loops

A nested for loop places one loop inside another, with the inner loop running fully for each iteration of the outer loop, commonly used for grids, patterns, and 2D array traversal.
Syntax
c
for (int outer = start; outer < limit; outer++) {
  for (int inner = start; inner < limit; inner++) {
    // inner body
  }
}

What is a Nested for Loop?

A nested for loop places one for loop entirely inside the body of another, and the inner loop runs through its full range once for every single iteration of the outer loop.

Example: What is a Nested for Loop?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Loop: repeat while the condition holds
	for (int i = 0; i < 2; i++) {
		// Loop: repeat while the condition holds
		for (int j = 0; j < 2; j++) {
			// Print formatted text to the screen
			printf("(%d,%d) ", i, j);
		}
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Printing a Grid Pattern

Nested loops are the standard technique for printing two-dimensional patterns like grids or triangles, with the outer loop typically controlling rows and the inner loop controlling columns within each row.

Example: Printing a Grid Pattern

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Loop: repeat while the condition holds
	for (int row = 0; row < 3; row++) {
		// Loop: repeat while the condition holds
		for (int col = 0; col < 3; col++) {
			// Print formatted text to the screen
			printf("* ");
		}
		// Print formatted text to the screen
		printf("\n");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Nested Loops over a 2D Array

Iterating over a two-dimensional array almost always uses nested loops, with the outer loop's index selecting a row and the inner loop's index selecting a column within that row.

Example: Nested Loops over a 2D Array

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int grid[2][2] = {{1, 2}, {3, 4}};
	// Loop: repeat while the condition holds
	for (int row = 0; row < 2; row++) {
		// Loop: repeat while the condition holds
		for (int col = 0; col < 2; col++) {
			// Print formatted text to the screen
			printf("%d ", grid[row][col]);
		}
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Nested Loops with Different Bounds

The outer and inner loops in a nested pair don't need matching bounds, and the inner loop's bound can even depend on the outer loop's current variable, which is how growing or shrinking patterns are produced.

Example: Nested Loops with Different Bounds

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Loop: repeat while the condition holds
	for (int i = 1; i <= 3; i++) {
		// Loop: repeat while the condition holds
		for (int j = 0; j < i; j++) {
			// Print formatted text to the screen
			printf("*");
		}
		// Print formatted text to the screen
		printf("\n");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Performance of Nested Loops

The total number of iterations in a nested loop is the product of the outer and inner loop counts, so nested loops over large ranges can quickly become expensive, an important consideration when reasoning about performance.

Example: Performance of Nested Loops

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare count and set it to 0
	int count = 0;
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		// Loop: repeat while the condition holds
		for (int j = 0; j < 4; j++) {
			count++;
		}
	}
	// Print formatted text to the screen
	printf("%d", count);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Reusing the same counter such as i in both the outer and inner loop, which breaks the counting.
  2. Using an inner bound that reads past the end of the row, such as j <= cols on an array with cols columns.
  3. Forgetting that the inner body runs outer times inner times, which makes big nested loops slow.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.