Skip to main content

Nested Loop in C

What are Nested Loops in C

A nested loop is a loop that is inside another loop.

The inner loop runs to completion for each iteration of the outer loop. This allows for the execution of a set of instructions for each combination of values from the two loops.

Nested loops are useful for tasks such as iterating over a two-dimensional array or processing all combinations of elements from two separate lists.

Here's an example of nested loops in C:

for (int i = 0; i < outerLimit; i++) {
for (int j = 0; j < innerLimit; j++) {
// code to be executed
}
}

Explanation

In this example, the outer loop is controlled by the variable "i" and runs from 0 to outerLimit-1, while the inner loop is controlled by the variable "j" and runs from 0 to innerLimit-1.

The code inside the inner loop will be executed innerLimit times for each iteration of the outer loop, resulting in a total of outerLimit * innerLimit executions.

note

You can use any valid C control structure inside any other control structure, including loops inside loops.

When nested loops should be used

Below are some of the basic scenarios when nested loops should be used:

  • Printing multiplication table: The outer loop iterates through the numbers 1-10 and the inner loop iterates through the numbers 1-10, and calculates the product of the two numbers for each iteration.

  • Printing a pyramid pattern: The outer loop iterates through the rows of the pyramid and the inner loop iterates through the columns of each row to print the necessary number of spaces and asterisks to create the pyramid shape.

  • Sum of all elements in a 2D array: A nested loop can be used to iterate through all elements of a 2D array and calculate the sum of all elements.

  • Counting the number of elements in a 2D array that meet a certain condition: A nested loop can be used to iterate through all elements of a 2D array and count the number of elements that meet a certain condition, such as being greater than a certain value.

  • Reversing the elements of a 2D array: A nested loop can be used to iterate through all elements of a 2D array and reverse the order of the elements.

  • Finding the largest element in a 2D array: A nested loop can be used to iterate through all elements of a 2D array, compare each element with a variable storing the current largest element and replace the variable with the larger element if necessary.

Nested loop Examples

Nested loop example using "for" loop

Here's an example of nested loops in C using for loop that prints a 3x3 multiplication table:

#include <stdio.h>

int main()
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
printf("%d x %d = %d\t", i, j, i * j);
}
printf("\n");
}
return 0;
}
Output:

Explanation

In this example, the outer loop is controlled by the variable "i" and runs from 1 to 3, while the inner loop is controlled by the variable "j" and runs from 1 to 3. The code inside the inner loop calculates the product of i and j for each iteration, and prints the result.

The inner loop will run 3 times for each iteration of the outer loop, resulting in a total of 3*3=9 print statements. The inner printf("\t") will create a tab after each iteration of the inner loop so the table is aligned properly.

note

You can adjust the range of the loops to create a multiplication table of any size.

This code will 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

Nested loop example using "while" loop

Below is the same example using while loops:

#include <stdio.h>

int main()
{
int i = 1, j = 1;
while (i <= 3)
{
while (j <= 3)
{
printf("%d x %d = %d\t", i, j, i * j);
j++;
}
printf("\n");
i++;
j = 1;
}
return 0;
}
Output:

This code will 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

This is the multiplication table of 3x3 using while loop

Nested loop example using "do-while" loop

Below is an example of nested loops in C that prints a 3x3 multiplication table using do-while loops:

#include <stdio.h>

int main()
{
int i = 1, j = 1;
do
{
do
{
printf("%d x %d = %d\t", i, j, i * j);
j++;
}
while (j <= 3);
printf("\n");
i++;
j = 1;
}
while (i <= 3);
return 0;
}
Output:

This code will 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

Challenges of using nested loops

There are many advantages of nested loops as they help us solve many complex problems in simple ways, but you should also be aware of challenges of using nested loops.

Some are listed below:

  • Nested loops can make the code more complex and harder to understand and debug.
  • Nested loops can lead to poor performance if not used carefully, especially when dealing with large data sets.
  • They can lead to infinite loop if there is no proper exit condition.
  • The code inside the nested loops can be hard to optimize for performance.
  • They can increase the risk of bugs and errors in the code.
  • They can lead to logical errors if the nested loops are not implemented correctly.