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

C में Nested for Loops

एक nested for loop एक loop को दूसरे के अंदर रखता है, inner loop outer loop की हर iteration के लिए पूरी तरह चलती है, आमतौर पर grids, patterns, और 2D array traversal के लिए इस्तेमाल होता है।
Syntax
c
for (int outer = start; outer < limit; outer++) {
  for (int inner = start; inner < limit; inner++) {
    // inner body
  }
}

Nested for Loop क्या है?

एक nested for loop एक for loop को पूरी तरह दूसरे की body के अंदर रखता है, और inner loop outer loop की हर single iteration के लिए अपनी पूरी range एक बार चलती है।

उदाहरण: 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;
}

एक Grid Pattern Print करना

Nested loops grids या triangles जैसे two-dimensional patterns print करने की standard technique हैं, outer loop आमतौर पर rows control करता है और inner loop हर row के अंदर columns control करता है।

उदाहरण: 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;
}

एक 2D Array पर Nested Loops

एक two-dimensional array पर iterate करना लगभग हमेशा nested loops इस्तेमाल करता है, outer loop का index एक row select करता है और inner loop का index उस row के अंदर एक column select करता है।

उदाहरण: 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;
}

अलग Bounds वाले Nested Loops

एक nested pair में outer और inner loops को matching bounds की ज़रूरत नहीं, और inner loop की bound outer loop के current variable पर भी depend कर सकती है, जो यह है कि बढ़ते या सिकुड़ते patterns कैसे produce होते हैं।

उदाहरण: 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;
}

Nested Loops की Performance

एक nested loop में iterations की total संख्या outer और inner loop counts का product है, इसलिए बड़ी ranges पर nested loops जल्दी costly हो सकते हैं, performance के बारे में सोचते समय एक important consideration।

उदाहरण: 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. #}
आम गलतियां
  1. outer और inner loop दोनों में same counter जैसे i दोबारा इस्तेमाल करना, जो counting तोड़ देता है।
  2. row के अंत से आगे पढ़ने वाला एक inner bound इस्तेमाल करना, जैसे cols columns वाले array पर j <= cols।
  3. यह भूल जाना कि inner body outer गुणा inner बार चलती है, जो बड़े nested loops को धीमा बनाता है।

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.