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 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;
}
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 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;
}
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 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;
}
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 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;
}
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 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;
}
Login to try C/C++/Java/PHP code in the editor
- outer और inner loop दोनों में same counter जैसे
iदोबारा इस्तेमाल करना, जो counting तोड़ देता है। - row के अंत से आगे पढ़ने वाला एक inner bound इस्तेमाल करना, जैसे
colscolumns वाले array परj <= cols। - यह भूल जाना कि inner body outer गुणा inner बार चलती है, जो बड़े nested loops को धीमा बनाता है।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: