# 7.11 – Nested loop

by on October 13, 2017

### Need of nested loop:

If we want to print the following output then a for loop which prints 1 to 5 natural numbers must be written for 3 times

1        2        3        4        5

1        2        3        4        5

1        2        3        4        5

```#include<stdio.h>
int main()
{
int i;
for(i=1;i<=5;i++)
printf("\t%d",i);
printf("\n\n");

for(i=1;i<=5;i++)
printf("\t%d",i);
printf("\n\n");

for(i=1;i<=5;i++)
printf("\t%d",i);
return 0;
}
```

Writing the same code for multiple times is a cumbersome job. Rather writing the same loop for 3 times, we can place the loop with in another loop which executes for three times.

```for(j=1;j<=3;j++)           /* run for 3 times */
{
for(i=1;i<=5;i++)   /* prints 1 to 5 natural numbers   */
printf("\t%d",i);
printf("\n\n");
}
```

The modified program would be

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

### Nested loops in C language:

• Placing a loop in another loop is called nested loop. Nested loops are used to solve complex problems.
• Inner loop executes completely once for every outer loop iteration, it happens as long as the outer loop condition is true.
• Say for example if inner loop iterates for ‘n’ times, outer loop iterates for ‘m’ times then total number of iterations of nested loop is m*n times. Now let us discourse some of the important issues involved in nested loops.

• Any loop can be placed in any loop that is a while loop can be placed in a for loop, a for loop can be placed in a while loop or a do-while loop can be placed in a for loop etc.
• The break statement in inner loop sends the control to the outer loop and resumes the execution of outer loop. The break statement in the outer loop terminates the execution of outer loop.
```#include<stdio.h>
int main()
{
int i,j;
for(j=1;j<=3;j++)
{
for(i=1;;i++)           /* infinite loop */
{
printf("\t%d",i);
if(i==j)
break;           /* goes to the outer loop */
}
printf("\n");
}
return 0;
}
```

Output:
1
1       2
1       2       3

Example explained:

When j is 1 then the inner loop executes only once to print 1 and comes out of the inner loop as the condition is if(1==1)
When j is 2 then the inner loop executes two times to print 1   2 and comes out of the inner loop as the condition is if(2==2)
When j is 3 then the inner loop executes three times to print 1  2  3 and comes out of inner loop as the condition is if(3==3)
When j is 4 then terminates the execution of outer loop as the outer loop condition is false.

• The goto statement is only the way to come out of the outer loop from the inner loop.
```#include<stdio.h>
int main()
{
int i,j;
for(j=1;;j++)          /* infinite loop */
{
for(i=1;;i++)           /* infinite loop */
{
printf("Hello World");
goto fox;        /* to come out of outer loop */
}
printf("\n");
}
fox:
return 0;
}
```

Output:
Hello World

• The continue statement in the inner loop sends the control to the conditional statement of inner loop. The continue statement in the outer loop sends the control to the conditional statement of outer loop.
```#include<stdio.h>
int main()
{
int i,j;
for(j=1;j<=5;j++)
{
for(i=1;i<=j;i++)
{
if(i%2==0)
continue;
printf("\t%d",i);  /* won't prints when i is an even */
}
printf("\n");
}
return 0;
}
```

Output:
1
1
1       3
1       3
1       3       5

Example explained:
Here outer loop sets the target to inner loop through i<j
In the inner loop when i is an even, the continue statement sends the control to the conditional statement before printing the term. Terms only print when i is an odd number.
There is no way to send the control to the conditional statement of outer loop from the inner loop.

Specification: Accept any integer and print its generic root (equivalent single digit number)

Logic:
Let us take an integer 18622
Sum of all the digits 1+8+6+2+2 is 19, again the sum of 1+9 is 10 and again the sum of 1+0 is 1.
Here 1 is the generic root of 18622. We sum individual digits of a number using the loop

```for(sum=0;n!=0;n=n/10)
sum=sum+n%10;
```

If sum is not a single digit number then we sore “sum” into “n”.

```for(sum=0;n!=0;n=n/10)
sum=sum+n%10;
n=sum;
```

We continue step 1 and 2 until we get a single digit number (n>9)

```do{
for(sum=0;n!=0;n=n/10)
sum=sum+n%10;
n=sum;
}while(n>9);     /* runs as long as n is not a single digit number*/
```

Program:

```#include<stdio.h>
int main()
{
int n,sum;
printf("Enter any integer:");
scanf("%d",&n);
do{
for(sum=0;n!=0;n=n/10)
sum=sum+n%10;
n=sum;
}while(n>9);
printf("The generic root is  %d",sum);
return 0;
}
```

Execution 1:
Enter any integer:18622
The generic root is 1

Execution 2:
Enter any integer:96
The generic root is 6

Execution 3:
Enter any integer:5
The generic root is 5

Previous post: