c
C Programming Tutorial : Introduction

C Loops: Nesting of Loops With Example

When a loop is written inside the body of another loop, then it is known as the nesting of loops. Any type of loop can be nested inside any other type of loop. For example, a For loop may be nested inside another for loop or inside a while or do-while loop. Similarly, while and do-while loops can be nested.

Example -1

/*P5.23 Program to understand nesting in for loop*/
#include<stdio.h>
#include<conio.h>
void main( )
{
    int i,j;
    for(i=1;i<=3;i++) / *outer loop* /
        {
            printf("i %d\n",i);

            for(j=1;j<=4;j++) /*inner loop*/

            printf ("j %d\t", j);
            printf("\n");
        }
    getch();
}

Output:
i = 1
j = 1 j = 2 j = 3 j = 4
i=2
j = 1 j = 2 j = 3 j = 4
i=3
j = 1 j = 2 j = 3 j = 4

 

Here for each iteration of the outer loop, the inner loop is executed 4 times.

The next program prints Armstrong numbers. Armstrong number is a three-digit number in which the sum of cube of all digits is equal to the number, for example, 371 is an Armstrong number since 371.

= 33+73+13=27+343+1

Example -2

/* Program to print Armstrong numbers from 100 to 999*/
#include<stdio.h>
#include<conio.h>

void main ()
{
    int num, n, cube, d, sum;
    printf ("Armstrong numbers are : \n");
    for(num=100;num<=999;num++) / *outer 100p* /
        {
            n=num;
            sum=O;
            whi1e(n>0) /* inner loop*/
                {
                    d=n%10;
                    n/=10;
                    cube=d*d*d; .
                    sum=sum+cube;
                }/*End of while loop*/
            if (num==sum)
            printf("%d\n",num);
        } /*End of for loop*/
	getch();
}
 

Here the outer for loop is used to generate numbers from 100 to 999, and the inner while loop is used to extract digits and then find the sum of the cube of those digits.

Example-3

/*Program to find the sum of digits of a number until the sum is reduced
to 1 digit. For example: 538769->38->11->2*/
#include<stdio.h>
#include<conio.h>

void main()
{
    long ·num;
    int dig, sum;
    printf ("Enter a number ");
    scanf(“%ld”, &num) ;
    printf(“%ld->“, num);
    do
        {
            for(sum=0;num!=0;num/=10)
                {
                    dig=num-%l0;
                    sum+=dig;
                }
            printf("%d\t",sum);
            num=sum;
        }while(num/10!=0);
    printf("\n");
    getch();
}

Enter a number: 789988
789988->49    13     4
Here the inner for loop is used to find the digits of the number.
 

Infinite Loops

The loops that go on executing infinitely and never terminate are called infinite loops. Sometimes we write these loops by mistake while sometimes we deliberately make use of these loops in our programs. Let us take some examples and see what type of mistakes lead to infinite loops.

Syntax-1

for (i=0; i<=5; i- -)
printf("%d",i); 

(A) This loop will execute until the value of i is less than or equal to 5 i.e. the loop will terminate only when i becomes greater than 5. The initial value of i is 0 and after each iteration, its value is decreasing, hence it will never become greater than 5. So the loop condition will never become false and the loop will go on executing infinitely. For the loop to work correctly we should write
i++ instead of i- -.

B) There should be a statement inside the loop body that changes the value of the loop variable after<-each iteration. In for loop, this work is done by. the update expression but in- while and do while we may forget to change the loop variable and this can lead to an infinite loop.

Syntax-2

int k=l;
do
{
printf("%d", k);
sum=sum+k;
}while(k<5);
 

Here we are not changing the value of k inside the loop body and hence the loop becomes infinite.

C) A common mistake made by beginners is to use the assignment operator(=) where equality operator(= =) should have been used. If this mistake is made in the loop condition then it may cause the loop to execute infinitely. For example, consider this loop:

Syntax-3

while(n=2)
{
……………
……………
}
 

Here we wanted the loop to execute till the value of n is equal to 2. So we should have written n= =2 but mistakenly we have written n = 2. Now n = 2 is an assignment expression and the value of this expression is 2, which is a nonzero(true) value and hence the loop condition is always true.

D). Everything seems to be correct with this loop but even then it executes infinitely. This is because i is an int variable and the range of an int variable is from -32768 to 32767. As the value of I exceeds 32767 it goes on the negative side and this process goes on, leading to an infinite loop.

Syntax-4

int i;
for(i=32000;i<=32767;i++)
printf("%d ",i);
 

Everything seems to be correct with this loop but even then it executes infinitely. This is because i is an int variable and the range of an int variable is from -32768 to 32767. As the value of I exceeds 32767 it goes on the negative side and this process goes on, leading to an infinite loop. The output is-

32000 32001……. 32767 -32768 -32767………….-1 0 1 2 ………….. 32767…………….. -32768…………….