Nested Loop

Nested Loop

A loop inside another loop is called the Nested loop. We can nest any number of a loop inside a loop to perform a specific task. But as we go with nested loops the speed of the program will be sacrificed, so use nested loop when it needed extremely.

For example, you want to print the following pattern...

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

In the sequence to print the above pattern, The first thought comes into your mind is how to print the first line (1 2 3 4 5) of the given pattern.

To print the first line we iterate the loop from 1 to 5 as given in the following code snippet.

for(i = 1; i <= 5; i++) {
    printf("%d ",i);
}

Now to print the above pattern we need to repeat the above code 4 times to print the same line for 4 times and change the line after each line. The following code snippet repeats the above code 4 times...

for(j = 1; j <= 4; j++) {
   for(i = 1; i <= 5; i++) {
      printf("%d ",i);
   }
   printf("\n");
}

In the above code snippet loop with counter, j is called outer loop and loop with counter i  is called the inner loop. Here as you can easily observe that the inner loop is used to print the numbers in one row and the outer loop is used to counter the rows.

If you think the above pattern in the tabular format, then observe the outer loop is used to count rows and inner loop is used to count / print columns.

The last thing you have to observe is that, as inner loop ends as it completes printing one row and after print one row to change the line we use printf("\n") to change the line and move the cursor to the new line after printing each row.

Program to print the left following pattern.

/**
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5
*/

#include<stdio.h>
int main() {
	int i,j;
	
	for(i = 1; i <= 5; i++) {
		for(j = 1; j <= i; j++) {
			printf("%d ",j);
		}
		printf("\n");
	}
	return 0;
}

Explanation

  1. If you observe the above pattern, you can easily see that the numbers in each row are incremented regularly.
  2. In the above pattern in 1st row 1 number gets printed, in 2nd row 2 numbers get printed, in 3rd row, 3 numbers get printed and so on. Means in the ith row i numbers should get printed.
  3. In the above code i is the counter for rows and j is the counter for numbers printed in each row.
  4. As in ith row, i numbers should printed so inner loop executed i times.

Program to print the table from 2 to 10, Each number table should printed in each single row.

/**
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
*/

#include<stdio.h>

int main() {
	int i,j;
	
	for(i = 2; i <= 10; i++) {
		for(j = 1; j <= 10; j++) {
			printf("%d ",i * j);
		}
		printf("\n");
	}
}

To understand the above program, suppose you want to print the table of 2 initially. To print the table of 2 we need to iterate a loop from 1 to 10 and multiple loop counter with 2 as shown in the following code snippet.

for(j = 1; j <= 10; j++) {
    printf("%d ",2 * j);
}

Now to print the table from 2 to 10 we need to run a loop of numbers from 2 to 10 and nest the inner loop inside that to the table of each number. Observe the following code snippet to clarify the concept

for(i = 2; i <= 10; i++) {
   for(j = 1; j <= 10; j++) {
       printf("%d ",i * j);
   }
   printf("\n");
}

Now it is clear that the outer loop is considered for different number runs from 2 to 10 and inner loop print the table of each number.

To print a table of each number in a single line, after completion of the inner loop line is changed.

Nested loop in C part-1 | Video Tutorial in Hindi

Nested loop in C part-2 | Video Tutorial in Hindi