Nested for-loop in C Language

This “Nested for-loop C language Tutorials ” page explains clearly how for-loop executes inside another for-loop using c language syntax with clear examples.


 

Nested for loop :

  • Defining for loop inside another for loop.
  • Inner fore loop executes only when inner loop condition is true.
  • Loop execution terminates when outer condition has failed.

Syntax:

for(outer_init ; outer_cond ; outer_modify)
{
Outer loop statements ;
for(inner_init ; inner_cond ; inner_modify)
{
Inner loop statements ;
}
Outer loop statements ;
}

 

#include<stdio.h>
void main()
{
    int i,j;
    for(i=5 ; i>=1 ; i–)
    {
        for(j=i ; j>=1 ; j–)
        {
            printf(“*”);
        }
        printf(“\n”);
    }
}


#include<stdio.h>
main()
{
    int i,j,n,k=0;
    printf(“enter no of rows : “);
    scanf(“%d”,&n);
    for(i=1 ; i<=n ; i++)
    {
        for(j=1 ; j<=i ; j++)
        {
            printf(“%d”,k++%10);
        }
        printf(“\n”);
    }
}

Share this