Thursday, 25 August 2016

Nested Loops


A loop containing another loop in its body is a Nested Loop. In Nested Loop the inner loop terminate before the outer loop.

Program:

//Assuming that all the header files are included
.
.
.
for(i=1; i<5; i++)    //Considering that variable i and j are already created
{
cout<<"\n";
for(j=1; j<=i; j++)
cout<<"*";
}
.
.
.

Output:

*
**
***
****

First the value of i is assigned as 1, then comes the inner loop j =1(also assigned). Now inner loop having j will run until the test expression becomes true and then value of i increases from 1 to 2. Then inner loop will run again and the will go on like this until the test expression of i becomes true.Then it will terminate.

No comments:

Post a Comment