Nested loops

You can place loops inside other loops is called Nested Loops.This can be useful, for example, when you are coding multidimensional arrays.Any kind of loop can be nested.

For example, the code below could have been written with while loops instead of for loops:

#include <stdio.h>
#define SIZE 5
int main()
{
  int square_y, square_x;
  printf ("\n");
  for (square_y = 1; square_y <= SIZE; square_y++)
  {
    for (square_x = 1; square_x <= SIZE; square_x++)
    {
      printf("*");
    }
    printf ("\n");
  }
  printf ("\n");
  return 0;
}

The output of the above code looks like this:

*****
*****
*****
*****
*****
Back