Nested Loops in C Programming

A nested loop is a loop inside another loop. The inner loop is executed completely for each iteration of the outer loop. Nested loops are commonly used for tasks like processing multi-dimensional arrays or creating patterns.


Syntax of Nested Loops

 

for (initialization; condition; increment/decrement)
{
// Outer loop block
for (initialization; condition; increment/decrement)
{
// Inner loop block
}
}

Nested loops can be of any type (for, while, or do-while) and can be combined (e.g., a for loop inside a while loop).


How Nested Loops Work

  1. The outer loop runs once.
  2. The inner loop runs completely for each iteration of the outer loop.
  3. When the inner loop completes, control goes back to the outer loop, and the next iteration starts.

Examples of Nested Loops

Example 1: Generating a Multiplication Table

#include <stdio.h>

int main() {
for (int i = 1; i <= 3; i++) { // Outer loop
for (int j = 1; j <= 3; j++) { // Inner loop
printf(“%d x %d = %d\n”, i, j, i * j);
}
printf(“\n”); // New line after each row
}
return 0;
}

Output:

1 x 1 = 1
1 x 2 = 2
1 x 3 = 3

2 x 1 = 2
2 x 2 = 4
2 x 3 = 6

3 x 1 = 3
3 x 2 = 6
3 x 3 = 9


Example 2: Printing a Number Pattern

#include <stdio.h>

int main() {
for (int i = 1; i <= 5; i++) { // Outer loop controls rows
for (int j = 1; j <= i; j++) { // Inner loop controls columns
printf(“%d “, j);
}
printf(“\n”); // New line after each row
}
return 0;
}

Output:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5


Example 3: Printing a Star Pattern

#include <stdio.h>

int main() {
for (int i = 1; i <= 5; i++) { // Outer loop controls rows
for (int j = 1; j <= i; j++) { // Inner loop controls columns
printf(“* “);
}
printf(“\n”); // New line after each row
}
return 0;
}

Output:

*
* *
* * *
* * * *
* * * * *


Example 4: Processing a 2D Array

#include <stdio.h>

int main() {
int matrix[2][2] = {{1, 2}, {3, 4}};

for (int i = 0; i < 2; i++) { // Outer loop for rows
for (int j = 0; j < 2; j++) { // Inner loop for columns
printf(“Element [%d][%d] = %d\n”, i, j, matrix[i][j]);
}
}
return 0;
}

Output:

Element [0][0] = 1
Element [0][1] = 2
Element [1][0] = 3
Element [1][1] = 4


Key Points About Nested Loops

  1. Execution Order:
    • The inner loop executes completely for each iteration of the outer loop.
  2. Performance:
    • Nested loops can be computationally expensive. If both loops have n iterations, the complexity is O(n2)O(n^2).
  3. Combining Loop Types:
    • You can mix loop types. For example, an outer for loop with an inner while loop.

Example: Mixed Loops

Using for Loop Outside and while Loop Inside

#include <stdio.h>

int main() {
for (int i = 1; i <= 3; i++) { // Outer loop
int j = 1;
while (j <= i) { // Inner loop
printf(“%d “, j);
j++;
}
printf(“\n”);
}
return 0;
}

Output:

1
1 2
1 2 3


Common Use Cases for Nested Loops

  1. Processing multi-dimensional arrays.
  2. Generating patterns.
  3. Performing matrix operations (e.g., addition, multiplication).
  4. Nested iterations for complex logic.

Summary

Loop Type Outer Loop Inner Loop
for Loop Controlled iteration Controlled iteration
while Loop Iterates until condition is true Iterates until condition is true
do-while Executes at least once Executes at least once

Leave a Comment

Your email address will not be published. Required fields are marked *