Nested for loop in c


  • In nested for loop, one or more for statements are included in the body of the loop.
  • In other words, C allows multiple for loops in nested forms. There is no ­boundary on the number on nested loops.

    Syntax of for loop:

    for(initialization,condition;increment/decrement)
    {
        for(initialization,condition;increment/decrement)
        {
            statement(s);
        }
     statement(s);
    }

    Example:

    #include <stdio.h>
    int main()
    {
       for (int i=0; i<2; i++)
       {
    	for (int j=0; j<4; j++)
    	{
    	   printf("%d, %d\n",i ,j);
    	}
       }
       return 0;
    }
    

    Output:

    0, 0
    0, 1
    0, 2
    0, 3
    1, 0
    1, 1
    1, 2
    1, 3