Thursday, October 11, 2018

What is a nested loop and how does a nested loop work?

What is a nested loop ?

  • A nested loop is a loop within a loop, if a loop have another loop inside the body then its called nested loop. 
  • How this works is that the first pass of the outer loop run the inner loop, which executes to completion. Then the second pass of the outer loop run the inner loop again. This repeats until the outer loop finishes.
  • Example of nested  for loop given below. 


#include <iostream.h>
#include <conio.h>
void main(void)
{  clrscr();
for(int a=1;a<=2;a++)
{
  for(int b=1;b<=3;b++)
   {
  cout<<"A is "<<a<<" & B is "<<b<<endl;
   }
}
getch();
}

No comments:

Post a Comment