C Programming for Beginners

Nested For Loop in C and Do While Loop

Nested For Loop and Do While Loop in C Language

Nested For Loop in C and Do While Loop

In the last chapter, we have studied simple FOR loop. In this chapter, we will study about nested loops.

Nested loop means loops inside loops.

Now suppose you want print square of stars

Here we need to travel two ways, one is row and another is column…..

In C language we first travel along the first row and then second row and so on…

Just write a small program to print 10 stars in a row,

Open our editor and write MAIN, declare variables i and j. We need two counter variables, since we will be running two loops.

Now write a FOR loop with j=1 ,j <= 10 and j++. Now print star using printf statement.

Lets run the program. Our program printed 10 stars in one line or we can say in a row.

main()
{
    int j;
    for (j=1; j <= 10; j++)
   {
        printf(“*”);
    }
}

You can view the source code from Newtum github repository  link over here.

Output

     **********

Now we need to print other rows. So we will put this loop in another loop.  To do this we write another loop with i counter , FOR then i=1 , i <=10 and i++.

With the help of curly brackets we will bring the j loop inside I loop, I and J loops don’t mean anything, they are just counter of the respective loops for your simple understanding.

Now after the j FOR loop we will use printf statement to print the next line using \n.Now run the program.

main()
{
int i, j;
for (j=1; j <= 10; j++)
{
for (i=1; i<= 10; i++)
{
printf(“* “);
}
printf(“\n”);

}
}

You can view the source code from Newtum github repository  link over here.

output :

* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *

This loop is called as nested loop , since we have 1 loop inside the other loop. Let’s iterate through the program

Well , first program reaches this line. Here I is set to 0 , and after checking condition it enters the J loop.

Now value of j becomes 0 and after checking condition it prints the first star over here.

After printing, the value of j becomes 2 and it again enters the J loop since condition is met and again prints the star. According to the condition J FOR loop is executed 10 times printing 10 stars. Now the J loop breaks.

And now it prints the new line using this printf statement. This printf statement is outside of the j loop but inside the i loop.

After first execution of j loop, the system goes to i increment over here. Are you getting everything over here? Please closely look at curly brackets, you will understand the meaning. Here I am highlighting them with different colors.

Now value of I becomes 2 , and it checks the condition. Well condition is true and system again enters the i loop.

Here we have FOR loop with j counter. Execution of j loop will restart. Value of j will become zero for the second time. It will print 10 stars in second line. After printing 10 stars it will go to next line because of this printf statement.

This process will go on till the first loop of I breaks. I hope you have understood some parts of this nested FOR loop.

Example :

main()
{
int i, j, k;
for(i=5;i>=1;i–)
{
for(j=5;j>i;j–)
{
printf(” “);
}
for(k=1;k<(i*2);k++)
{
printf(“* “);
}
printf(“\n”);
}
return 0;
}

Lets run the program.

You can view the source code from Newtum github repository  link over here.

Output :

* * * * * * * * *
* * * * * * *
* * * * *
* * *
*

Now suppose you want to print series of number.  1 to 10 numbers in each row. How will you do this?

Let’s open our source code of nested FOR loop again. In this loop we have printed stars. But now we need to print the numbers.

Here we just have one printf statement, let’s modify it to print the value of j. 

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

Lets run our program. And this is our output as expected.

Output :

12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910

Well here again for every execution of first loop this second loop is executed 10 times. And the printf statement with \n  puts all the numbers in next row every time. Now suppose you want to print a more complicated series.

Here we have just 1 in first row, 1 and 2 in the second row. And 1, 2, 3 in the third row and so on..

How to program this? Let’s open our program again. Let me tell you one thing over here, here first loop stands for row and second loop stands for column. That means i stands for row and J stands for column.

In our earlier example of numbers, for every value of i, j loop printed values in all the columns. But in the current scenario we want to print only 1 number in the first row and two numbers in the second row and so on.

Here we can say that, when value of i is 1 , j Loop will print only 1 value. When the value of i is 2,  j loop will print two values and so on.

So let’s open our source code again of the last example.

Here we will modify the loop j so that it will depend on the value of i. Lets modify the condition, earlier condition was j < 10 , now we will modify the condition to j < i.

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

Lets run the program. Yes, we got the correct output.

You can view the source code from Newtum github repository  link over here.

Output :

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10

Let’s iterate through the example. Please look at the table.

When we run the program and reach the first FOR loop, the value of i is 1. It checks the condition and enters inside loop.

Now the value of J is 1. System checks whether the value of J is less than equal to 1 or not. Answer is true. And it prints 1 i.e. value of J.

Now system goes to increment counter of J loop and value of J becomes 2. Now when it goes to condition J <= I , condition fails and system breaks the loop and goes to printf statement, which puts the next line character.

Here system will go to the increment counter of i and value of i becomes 2.

Now again the J loop will execute, but twice. Since the value of i is 2 and j loop condition is j <= 2 and so it will print 2 values. And so on.

Hope you have understood the concept of Nested FOR loop. We request you to execute lots of examples to get better hold on Nested FOR loop.

Example 1 :

main()
{
int i , j;
for(i = 10 ; i >= 1 ; i–)
{
for(j = 1 ; j <= i ; j++)
{
printf(“%d “,j);
}
printf(“\n”);
}
}

Some other examples 1 on number pattern You can view the source code from  Newtum github repository  link over here.

Output :

1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Example 2 :

main()
{
int i, j, k;
for(i=10 ; i>=1 ; i–)
{
for(j=10 ; j>i ; j–)
{
printf(” “);
}
for(k=1 ; k<=i ; k++)
{
printf(“%d “,k);
}
printf(“\n”);
}
}

examples 2 on number pattern You can view the source code from  Newtum github repository  link over here.

Output :

1 2 3 4 5 6 7 8 9 10
   1 2 3 4 5 6 7 8 9
      1 2 3 4 5 6 7 8
         1 2 3 4 5 6 7
            1 2 3 4 5 6
               1 2 3 4 5
                  1 2 3 4
                     1 2 3
                        1 2
                           1

DO WHILE loop

Now let’s have a look at one more type of loop; called as “do while” loop. DO WHILE loop is similar to WHILE loop. Only difference in DO WHILE loop is the condition is written at the end.

Technical difference is even if the condition is wrong DO WHILE loop is executed at least once.

Like any other loop we need initialization , condition and increment to execute the loop.

Lets execute an example.

We will write MAIN, declare a variable i. Set the value of i as 20. Write DO, followed by curly brackets. Inside DO WHILE loop we will print the value of i. And then we will write the condition that i < 10.

It’s weird over here, we have set the value of i as 20, but we have written the condition as i < 10. Don’t worry, we have done this to explain DO WHILE loop.

From the loop structure we can clearly say that the loop will never execute. But it will run and print the value of i. Because do-while loops are executed at least once even if the condition is false.

main()
{
int i=20;
do
{
printf(” %d”, i);
i++;
}while (i<10);
}

You can view the source code from Newtum github repository  link over here.

Output :

             20

Lets run the program, well you got the output. From the output you can confirm that DO WHILE loops are executed at least once, even if the condition is false.

Everything else in DO WHILE is same as WHILE loop and it’s used in very rare cases.

Let’s summarize the loops.  Loop statements are used to execute the same logic “n” number of times. There are three important things for loops. Initialization, Condition and Increment/Decrement. 

Types of loops

  1. WHILE Loop,
  2. FOR Loop and
  3. DO WHILE Loop

WHILE Loop

In WHILE Loop, we write initialization , condition and increment/decrement in 3 different lines.

FOR Loop

In FOR Loop, we write initialization , condition and increment/decrement in the same line.

DO WHILE

In DO WHILE , condition is written at the end and its executed at least once, no matter what the result of condition is. We have nested loop i.e. loop inside another loop to execute complicated logic.

Well this was all about loops. We assume you will write lots of examples to get a better hold on it.

We hope this was not complicated for you as we have tried our best to make it as simple as possible.

 

Facebook Comments
(Visited 1 times, 1 visits today)