← Back to C++ Course | Chapter 4: Control Flow | Lesson 12 of 13

C++ में Nested for Loops

एक nested for loop एक loop को दूसरे के अंदर रखता है, inner loop outer loop की हर iteration के लिए पूरी तरह चलती है, आमतौर पर grids, patterns, और 2D array traversal के लिए इस्तेमाल होता है।
Syntax
cpp
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?

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Loop: repeat while the condition holds
	for (int i = 1; i <= 2; i++) {
		// Loop: repeat while the condition holds
		for (int j = 1; j <= 3; j++) {
			// Print to the console with cout
			std::cout << i << "-" << j << " ";
		}
	}
	// Print to the console with cout
	std::cout << std::endl;
	// 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

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Loop: repeat while the condition holds
	for (int row = 1; row <= 3; row++) {
		// Loop: repeat while the condition holds
		for (int col = 1; col <= 3; col++) {
			// Print to the console with cout
			std::cout << "* ";
		}
		// Print to the console with cout
		std::cout << std::endl;
	}
	// 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

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
	// Loop: repeat while the condition holds
	for (int row = 0; row < 2; row++) {
		// Loop: repeat while the condition holds
		for (int col = 0; col < 3; col++) {
			// Print to the console with cout
			std::cout << grid[row][col] << " ";
		}
	}
	// Print to the console with cout
	std::cout << std::endl;
	// 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

cpp
// Include std::cout and std::cin
#include <iostream>

// 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 = 1; j <= i; j++) {
			// Print to the console with cout
			std::cout << "* ";
		}
		// Print to the console with cout
		std::cout << std::endl;
	}
	// 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

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare outerCount and set it to 100
	int outerCount = 100;
	// Declare innerCount and set it to 100
	int innerCount = 100;
	long totalIterations = (long)outerCount * innerCount;
	// Print to the console with cout
	std::cout << "Total iterations: " << totalIterations << std::endl;
	// 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. inner और outer loop दोनों में same variable जैसे i दोबारा इस्तेमाल करना, जो counting तोड़ देता है।
  2. bounds गलत करना ताकि inner loop array के अंत से आगे index करे।
  3. यह notice न करना कि inner body outer गुणा inner बार चलती है, जो programs को बहुत धीमा बना सकता है।

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.