Nested loops in C are loops inside other loops. They are useful for performing tasks that require multiple iterations for each iteration of the outer loop, such as working with multi-dimensional arrays or creating patterns.
Syntax of Nested Loops
A nested loop has the following structure:
for (initialization; condition; increment) { // Outer loop code for (initialization; condition; increment) { // Inner loop code } // Outer loop continues }
You can also have other types of loops (like while
or do...while
) nested inside for
loops and vice versa.
Example 1: Nested for
Loops
Here’s an example of using two for
loops to print a 2D grid:
#include <stdio.h> int main() { for (int i = 1; i <= 3; i++) { // Outer loop for (int j = 1; j <= 3; j++) { // Inner loop printf("i = %d, j = %d\n", i, j); } } return 0; }
Output:
i = 1, j = 1 i = 1, j = 2 i = 1, j = 3 i = 2, j = 1 i = 2, j = 2 i = 2, j = 3 i = 3, j = 1 i = 3, j = 2 i = 3, j = 3
In this example:
- The outer loop (
i
) runs 3 times. - For each iteration of the outer loop, the inner loop (
j
) runs 3 times. - This results in 9 total iterations (3 outer iterations × 3 inner iterations).
Example 2: Printing a Multiplication Table (2D Array)
A common use of nested loops is to print multiplication tables.
#include <stdio.h> int main() { for (int i = 1; i <= 5; i++) { // Outer loop for rows for (int j = 1; j <= 5; j++) { // Inner loop for columns printf("%d\t", i * j); // Print multiplication of i and j } printf("\n"); // Move to the next line after each row } return 0; }
Output:
1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25
Here:
- The outer loop controls the rows (from 1 to 5).
- The inner loop controls the columns (from 1 to 5).
- For each combination of
i
andj
, the multiplication result is printed.
Example 3: Nested while
Loops
You can nest while
loops inside each other as well. Here’s an example that prints a pattern using nested while
loops.
#include <stdio.h> int main() { int i = 1; while (i <= 3) { // Outer loop int j = 1; while (j <= 3) { // Inner loop printf("* "); j++; } printf("\n"); i++; } return 0; }
Output:
* * * * * * * * *
Here:
- The outer
while
loop runs 3 times. - The inner
while
loop also runs 3 times for each iteration of the outer loop, printing*
each time.
Example 4: Nested do...while
Loops
You can also nest do...while
loops. Here’s an example of printing a pattern with nested do...while
loops:
#include <stdio.h> int main() { int i = 1; do { // Outer loop int j = 1; do { // Inner loop printf("* "); j++; } while (j <= 3); printf("\n"); i++; } while (i <= 3); return 0; }
Output:
* * * * * * * * *
This example is similar to the previous one, but it uses do...while
loops instead of while
loops. The outer loop runs 3 times, and the inner loop also runs 3 times for each iteration of the outer loop.
Example 5: Using Nested Loops for 2D Arrays
Nested loops are often used to process elements of 2D arrays (arrays of arrays). Here’s an example where we declare a 2D array and print its elements:
#include <stdio.h> int main() { int arr[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; for (int i = 0; i < 3; i++) { // Outer loop for rows for (int j = 0; j < 3; j++) { // Inner loop for columns printf("%d ", arr[i][j]); // Accessing elements of the array } printf("\n"); } return 0; }
Output:
1 2 3 4 5 6 7 8 9
In this example:
- The outer loop iterates over the rows of the 2D array.
- The inner loop iterates over the columns of each row.
- We access each element of the array using
arr[i][j]
.
Example 6: Using break
and continue
in Nested Loops
You can use break
and continue
within nested loops to control their flow. Here’s an example that prints a multiplication table but skips the iteration when i * j
equals 12:
#include <stdio.h> int main() { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) { if (i * j == 12) { continue; // Skip the iteration when the product is 12 } printf("%d\t", i * j); } printf("\n"); } return 0; }
Output:
1 2 3 4 5 2 4 6 8 10 3 6 9 11 15 4 8 12 16 20 5 10 15 20 25
In this example:
- The
continue
statement skips the printing of12
wheni * j
equals 12.
Summary
- Nested loops are loops placed inside another loop, and they are commonly used when dealing with multi-dimensional arrays or when you need to perform repetitive tasks that involve multiple variables.
- You can nest
for
,while
, anddo...while
loops within each other. - The flow of nested loops can be controlled using
break
andcontinue
. - Nested loops are a powerful tool but should be used carefully to avoid unnecessarily complex or inefficient code, especially for large datasets.