You can create code that embeds one loop inside another loop. This is called a nested loop. Any loop type can be nested within another type; the most common is the nesting for loops.

Nested for loops are used to cycle through matrix/tabular data and multi-dimensional arrays. Since a table is a matrix of rows and columns, you need one loop to loop through the row, then across the column. Another use for nested loops is in mathematical functions, where you need to conduct complex evaluations on data.

To nest a loop, you insert the new loop within the brackets of the first loop.

Nested for loop
Nested for loop
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
     {
          for(j=1;j<=i;j++)
             {
                  printf("%d ",j);
              }
       printf("\n");
     }
}