C++ में Nested for Loops
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
एक 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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
एक 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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
अलग 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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Nested Loops की Performance
एक nested loop में iterations की total संख्या outer और inner loop counts का product है, इसलिए बड़ी ranges पर nested loops जल्दी costly हो सकते हैं, performance के बारे में सोचते समय एक important consideration।
उदाहरण: Performance of Nested Loops
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- inner और outer loop दोनों में same variable जैसे
iदोबारा इस्तेमाल करना, जो counting तोड़ देता है। - bounds गलत करना ताकि inner loop array के अंत से आगे index करे।
- यह notice न करना कि inner body outer गुणा inner बार चलती है, जो programs को बहुत धीमा बना सकता है।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: