Introduction to Nested Loop in C
As the name already suggests, a loop inside a loop is called Nested Loop. There can be any number of loops inside a loop. We know there are generally many looping conditions like for, while, and do-while. We can loop different kinds of loops within each other to form nested loops. C language supports this functionality of Nested Loops. below is the syntax of Nested Loop in C.
Syntax:
Outside_loop
{
//Outside Loop Statements
Inside_loop
{
//Inside loop Statements
}
}
The above syntax is a single loop condition inside a loop condition. In this way, there can be many conditions too.
Outside_loop
{
//Outside Loop Statements
Inside_loop_1
{
//Inside loop 1 Statements
}
Inside_loop_2
{
//Inside loop 2 statements
}
Inside_loop_3
{
//Inside loop 3 statements
}
……… continues
}
Flowchart of Nested Loop
Here, let us see the actual process flow in the case of these nested loops.
In the above flow chart, we can see that there are two conditions that are given. The inner loop condition gets executed only when the outer loop condition gives the Boolean output as True. Else the flow control directly goes out of both the loops. Now coming into the inner loop execution, If the loop condition gives a true result, then the block of statements under that loop and the incremental condition gets executed. And in turn, if the condition gives a Boolean condition as False, then the inner loop gives its control back to the outer loop, and again same conditions/loops gets executed/repeated.
Examples to Implement Nested Loop in C
Let us see below a few examples of the functionality of nested for loops in C and understand how it works through programs.
Example #1
Nested loop in ‘for’ condition. This we can generally use for creating or printing a multi-dimensional array.
Code:
#include <stdio.h>
int main()
{
int i,j,x,y;
int a[10][10];
printf("Enter value for x(rows)- max of 10: ");
scanf("%d", &x);
printf("Enter value for y(columns) - max of 10: ");
scanf("%d",&y);
printf("Let's create a 2-D array: ");
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Now printing the array: ");
printf("\n");
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
printf("\t");
printf("%d",a[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Let us see how the above example code works:
- Firstly, we declare the integer values for defining the number of rows and columns.
- Next, the array declaration is done.
- We then have to take the inputs from the user as per the values specified for the number of rows and columns.
- The user input is taken with the help of ‘two for loops’ as we are creating a 2-D array.
- The first ‘for-loop’ is for the number of rows and the second loop is for the columns.
- In taking a user input for an array, we are considering it as a row by row concept.
- So, when all the columns in the first row are completely filled, the compiler point would then increment come to the next row by which all the columns are filled, and the process continues.
- The same process flow continues for the loops for printing the respective output in an array format.
In this manner, the nested loops are implemented. Now, let us have another example for nested loops.
Example #2
Code:
#include <stdio.h>
int main()
{
int x,y;
int k=1;
printf("Enter the number of rows: ");
scanf("%d", &x);
printf("Enter the number of columns: ");
scanf("%d", &y);
int a[x][y];
int i=1;
while(i<=x)
{
int j=1;
while(j<=y)
{
printf("%d\t",k);
k++;
j++;
}
i++;
printf("\n");
}
}
As seen above, we had created another 2-D array using “while loop”.
Output:
The same level compilation as to the ‘for loop’ is being done. Once the outer while loop gets a Boolean “True” as the output, the next compilation code goes into the inner condition. Once the inner condition gives the output as “False”, the assignment again reaches the outer loop condition.
Example #3
Here, we will have a small inter mixture of for loops program.
Code:
#include <stdio.h>
int main()
{
int n=1;
int i;
while(n<5)
{
printf("*");
printf("\n");
n=n+1;
for(i=1;i<n;i++)
{
printf("$");
}
}
}
Output:
In the above program, as you have noticed, we had printed two different symbols, one after the other using while and for loop together. The combination of using different nested loops plays an important role in writing different level programs.
Example #4
Let us even look into an example dealing with the do-while nested loop. This example also lets print some random pattern.
Code:
#include <stdio.h>
int main()
{
int n=1;
int i=0;
do
{
printf("$");
printf("\n");
n=n+1;
do
{
printf("*");
i=i+1;
}while(i<n);
}while(n<5);
}
Output:
In the above program also, we have used nested do-while loops to print a pattern based on the given inputs.
Conclusion
Here, we got the basic syntax and understood a few examples with respect to different nested functions. We had learned how actually there would be the process flow through a flow chart and explained the working of a nested ‘for’ loop. So, keep practicing and enjoy learning C.
Recommended Articles
This is a guide to Nested Loop in C. Here we discuss the Introduction to Nested Loop in C and its examples, along with the flowchart of the nested loop. You can also go through our other suggested articles to learn more –