Best use of Nested loop and how to implement it

Best use of Nested loop and how to implement it
Techiio-author
Written by Shuvhojit DebFebruary 17, 2022
9 min read
C
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

We will learn how to implement the nested loop in C, its diagram, and an example in this blog post.

Defination of Nested loop in C :

A nested loop is a loop within a loop, an inner loop within the body of an outer one. How this works is that the first pass of the outer loop triggers the inner loop, which executes to completion. Then the second pass of the outer loop triggers the inner loop again. This repeats until the outer loop finishes. Of course, a break within either the inner or outer loop would interrupt this process.

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.

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
}
Flow Diagram:

blogpost

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.

Implementation of Nested loop in C:

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:-

blogpost

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 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:-

blogpost

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 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:-

blogpost

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 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:-

blogpost

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 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.

C Language
Programming
Nested Loop
Loops in C
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance