Pages

Wednesday, 9 May 2018

Nested loops in C


Nested loops in C
As I said in my earlier tutorials, nesting means defining statement under the scope of another similar statement. In case of loops, when we nest two loops then it generally multiplies the execution frequency of loops.

We can nest for loop inside while loop and vice versa is also true. Generally programmer nest up to 3 loops. But there is no limit of nesting in C.

Now let’s try a small program with nested loops.


#include<stdio.h>

void main()
{
 int row,col;
 for(row=1;row<4;row++)
 {
  for(col=1;col<4;col++)
  {
   printf("%d\t%d\n",row,col);
  }
 }
}
#include<stdio.h>

void main()
{
 int row,col;
 for(row=1;row<4;row++)
 {
  for(col=1;col<4;col++)
  {
   printf("%d\t%d\n",row,col);
  }
 }
}