Nested For Loop in C

SPONSOR AD

Nested For Loop in C Language with Examples

In this article, I will discuss the Nested For loop in C Language with Examples. Please read our previous articles discussing the For Loop in C Language with Examples.

Nested For Loop in C Language:

A nested for loop in C is a for loop inside another for loop. This is a common practice when dealing with multi-dimensional data structures like matrices or for performing operations that require multiple levels of looping. In a nested for loop, the number of iterations will equal the number of iterations in the outer loop multiplied by the number of iterations in the inner loop. The syntax of a nested for loop is essentially a for loop within the body of another for loop.

for (initialization1; condition1; increment1) {
    // Outer loop body

    for (initialization2; condition2; increment2) {
        // Inner loop body
    }
}

It uses two for loop:

  • Outer Loop: The first for loop is called the outer loop. Its initialization, condition, and increment affect how many times the inner loop will be executed in total.
  • Inner Loop: The second for loop is nested inside the outer loop. For each iteration of the outer loop, the inner loop goes through its entire cycle of initialization, condition checking, loop body execution, and increment.
Execution Flow of Nested For Loop in C:
  • Initialization of Outer Loop: The initialization1 part of the outer loop runs only once at the beginning.
  • Condition Check of Outer Loop: The condition1 is evaluated. If it’s true, the execution enters the outer loop; otherwise, the loop ends.
  • Entering the Inner Loop:
    1. Inner Loop Initialization: The initialization2 of the inner loop is executed.
    2. Inner Loop Condition Check: The condition2 is evaluated. If true, the execution enters the inner loop body; if false, it exits the inner loop and proceeds to the increment1 of the outer loop.
  • Executing the Inner Loop Body: If the inner loop’s condition is true, the code inside the inner loop body is executed.
  • Inner Loop Increment: After the inner loop body executes, the increment2 part is executed, and the control goes back to the inner loop’s condition check (condition2).
  • Repeating Inner Loop: Steps 4 and 5 repeat until the condition2 becomes false.
  • Outer Loop Increment: Once the inner loop completes (i.e., condition2 becomes false), the control goes to the increment1 part of the outer loop.
  • Repeating the Whole Process: The entire process (starting from step 2) repeats until the condition1 of the outer loop becomes false.

Here’s an example to illustrate the flow:

SPONSOR AD
#include <stdio.h>

int main() {
    int i, j;
    for (i = 1; i <= 3; i++) { // Outer loop
        printf("Outer Loop i = %d\n", i);

        for (j = 1; j <= 2; j++) { // Inner loop
            printf("\tInner Loop j = %d\n", j);
        }
    }
    return 0;
}
Execution Flow of the Example:
  • i is initialized to 1.
  • Outer loop checks i <= 3, which is true.
  • Enter outer loop, print i.
  • j is initialized to 1.
  • The inner loop checks j <= 2, which is true.
  • Enter inner loop, print j.
  • Increment j, now j is 2.
  • Check j <= 2, still true.
  • Execute and print j again.
  • Increment j, now j is 3.
  • Check j <= 2, it’s false. Exit the inner loop.
  • Increment i, now i is 2.
  • Repeat steps 2-12 until i becomes greater than 3.
Nested For Loop Examples in C Language

Nested for loops in C are loops within loops, often used for handling multi-dimensional data structures like arrays or matrices. Here are a few examples demonstrating different use cases of nested for loops in C:

Example: Multiplication Table

Let’s create a program in C that uses nested for loops to print a multiplication table:

#include <stdio.h>

int main() {
    int i, j;
    for (i = 1; i <= 10; i++) {
        for (j = 1; j <= 10; j++) {
            printf("%d x %d = %d\t", i, j, i * j);
        }
        printf("\n"); // New line after each row
    }
    return 0;
}

In this example:

  • The outer loop (for (i = 1; i <= 10; i++)) iterates 10 times, representing the rows of the multiplication table.
  • The inner loop (for (j = 1; j <= 10; j++)) iterates 10 times for each iteration of the outer loop, representing the columns of the table.
  • For each iteration of the inner loop, a multiplication (i * j) is performed and printed.
  • After each full cycle of the inner loop, the outer loop moves to the next iteration, causing a new line (printf(“\n”)) to be printed, thus forming a new row of the multiplication table.
Example: Printing a Matrix

This nested loop prints a 3×3 matrix.

#include <stdio.h>

int main() {
    int i, j;
    for (i = 1; i <= 3; i++) {
        for (j = 1; j <= 3; j++) {
            printf("%d ", i * j);
        }
        printf("\n");
    }
    return 0;
}
Example: Accessing 2D Array Elements

This example iterates through a 2D array and prints its elements.

#include <stdio.h>

int main() {
    int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
    int i, j;
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 3; j++) {
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }
    return 0;
}
Example: Pattern Printing

This nested loop prints a simple star pattern.

#include <stdio.h>

int main() {
    int i, j;
    for (i = 1; i <= 5; i++) {
        for (j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}
Example: Nested Loops with Break

Demonstrates using a break in a nested loop.

SPONSOR AD
#include <stdio.h>

int main() {
    int i, j;
    for (i = 1; i <= 3; i++) {
        for (j = 1; j <= 3; j++) {
            if (j == 2) break; // Breaks the inner loop
            printf("(%d, %d) ", i, j);
        }
        printf("\n");
    }
    return 0;
}

In the next article, I will discuss Entry Controlled Loop vs Exit Controlled Loop in C Language with examples. In this article, I try to explain Nested For Loop in C Language with examples. I hope you enjoy this article on Nested For Loop in C Language with Examples. I would like to have your feedback. Please post your feedback, questions, or comments about this article.

Leave a Reply

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