Full Stack Developer
C backings nesting of loops in C. Nesting of loops is the component in C that permits the looping of proclamations inside another loop. We should notice an instance of nesting loops in C.
Quite a few loops can be characterized inside another loop, i.e., there is no limitation for characterizing quite a few loops. The nesting level can be characterized at n times. You can characterize any kind of loop inside another loop; for instance, you can characterize 'while' loop inside a 'for' loop.
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
}
In the above stream graph, we can see that two circumstances are given. The inward loop condition gets executed just when the external loop condition gives the Boolean result is True. Else the stream control straightforwardly leaves both the loops. Presently coming into the internal loop execution, If the loop condition gives a genuine outcome, then, at that point, the square of articulations under that loop and the gradual condition gets executed. Also thusly, if the condition gives a Boolean condition as False, the inward loop gives its control back to the external loop, and again same circumstances/loops get executed/rehashed.
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:
In this manner, the nested loops are implemented. Now, let us have another example for nested loops.
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.
Here, we will have a small intermixture 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.
Let us even look into an example dealing with the do-while nested loop. This example also let's print some random patterns.
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.
Here, we got the essential language structure and comprehended a couple of models concerning different settled capacities. We had figured out how there would be the cycle course through a stream diagram and clarified the working of a settled 'for' loop. Along these lines, continue rehearsing and appreciate learning C.