Avinash Singh Programming,C Programming Nested Loops in C Programming

Nested Loops in C Programming

Nested Loops in C Programming post thumbnail image

In this post on Nested Loops in C Programming, we will learn how you can use nested for loop, nested while loop, and nested do while loop in C programming.

To understand nested loops, it is essential that you know how to use for, while, and do while first, so if you have not read the tutorials before this, then definitely read them first.

Like we used the if inside the if, i.e. nested if, similarly when we use the loop again inside a loop, we call it a nested loop.

We call the outer loop the outer loop, the loop that is inside the outer loop, we call the inner loop.

Mostly the inner loop is used to run the statements and the outer loop controls how many times the inner loop will run.

You can use any loop of the same or another type as a nested loop inside any for, while, or do while loop.

You can also read: do While Loop In C Programming

Nested Loops in C Programming(Nested for loop syntax):

for(initialization; condition;update) 
{ 
    // statement of outer loop 

    for(initialization; condition; update) 
    { 
           // statement of inner loop 
    } 

    // statement of outer loop 
}

Nested Loops in C Programming (Nested while loop syntax:)

while(condition)
{
    // statement of outer loop

    while(condition)
    {
           // statement of inner loop

           //Update statement of Inner Loop;
    }

    // statement of outer loop

    // Update statement of lower loop;
}

Nested Loops in C Programming(Nested do while loop syntax:)

do
{
    // statement of outer loop

    do
    {

           // statement of inner loop

           // Update statement of inner loop

    }while(condition);

    // statement of outer loop

    // Update statement of outer loop
}while(condition);

Nested Loops in C Programming(Mix Nested loop syntax):

while(condition)
{
    // statement of outer loop

    for(initialization; condition; increment/decrement)
    {
          // statement of inner loop

          //Update statement of for loop
    }

    do
    {
          //Statement of do-while loop
          //Update statement of do-while loop
    }

    while(condition);
    // statement of outer loop
    // Update statement of while loop
}

With all the above syntax, you must have got a little idea of ​​nested loops, but to understand nested loops in detail, we will understand nested for loop well and from this you will get to know about nested while loop and nested do while loop.

Nested for loop in C Programming (Flow):

Nested Loops in C Programming
Nested Loops in C Programming

Nested Loops in C Programming (Nested for loop execution process:)

Step 1: Nested for loop starts with variable initialization of outer loop, then control goes on condition of the outer loop (step 2).

Step 2: If the condition of the outer loop becomes true then the control of the program comes inside the outer loop.

Step 3: Now as you can see in the body of the outer loop we have used a nested loop. Now the statements of the body of this inner loop will be repeated until the condition of the inner loop becomes false.

Step 4: When the condition of the inner loop becomes false then the control of the program will come outside the inner loop but still the control of the program is inside the outer loop.

Step 5: Therefore the control of the program will go to the update of the outer loop where there will be an increment or decrement or any other update process in the value of the variable and then again the control of the program will go to step 2.

After step 2, the control of the program will go to step 3, step 4, and then step 5 and this will continue till the condition of the outer loop becomes false at step 2. 

Note

Program control will never go to step 1.

Nested Loops in C Programming (Nested for loop example program 1:)

#include <stdio.h>
int main()
{
    int i,j;
 
    for(i=1;i<=3;i++)
    {
        printf("\nI am Outer Loop : i = %d\n",i);
 
        for(j=1;j<=4;j++)
        {
            printf("I am Inner Loop : i = %d, j = %d\n",i,j);
        }
    }
 
    return 0;
}

Output:

I am Outer Loop : i = 1
I am Inner Loop : i = 1, j = 1
I am Inner Loop : i = 1, j = 2
I am Inner Loop : i = 1, j = 3
I am Inner Loop : i = 1, j = 4

I am Outer Loop : i = 2
I am Inner Loop : i = 2, j = 1
I am Inner Loop : i = 2, j = 2
I am Inner Loop : i = 2, j = 3
I am Inner Loop : i = 2, j = 4

I am Outer Loop : i = 3
I am Inner Loop : i = 3, j = 1
I am<strong> </strong>Inner Loop : i = 3, j = 2
I am Inner Loop : i = 3, j = 3
I am Inner Loop : i = 3, j = 4

Nested Loops in C Programming (Nested for loop example program 2:)

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

Output:

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

Conclusion:

I hope that you have liked this post of Nested Loops in C Programming. If you liked the post, then please share it with your friends and relatives and you can send your suggestion in the comment box below or email me at praveen171086@gmail.com.

You can also visit my Hindi YouTube Channel.

mm
Avinash Singh

I am Avinash Singh, a software engineer from the profession and have been working in the IT industry since 2009. I live in Barabanki district of Uttar Pradesh in India and completed intermediate in 2001 from Ram Sevak Inter College, a UP Board school. After inter I completed my professional degree (BCA & MCA) from Indira Gandhi National Open University, Delhi in 2008 and along with it I did many certifications to keep my knowledge up to date and the next year I started a software development company E – Vision Technocraft Pvt Ltd.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post