C Nested Loops
Nested Loops
In C programming, a nested loop refers to a loop (like for, while, or do-while) placed entirely within another loop.
The 'inner loop' will be executed one time for each iteration of the 'outer loop' defines a nested loop:
Example
#include <stdio.h>
int main() {
int i, j;
// Outer loop
for (i = 1; i <= 2; ++i) {
printf("Outer: %d\n", i); // Executes 2 times
// Inner loop
for (j = 1; j <= 3; ++j) {
printf(" Inner: %d\n", j); // Executes 6 times (2 * 3)
}
}
return 0;
}
Output
Outer: 1
Inner: 1
Inner: 2
Inner: 3
Outer: 2
Inner: 1
Inner: 2
Inner: 3