Nested Loops in C programming language

In this article, we will learn about different types of nested loops in C programming language with their syntaxes, examples.
Submitted by Sneha Dujaniya, on July 19, 2018

Nesting of Loops

A loop inside another loop is called nesting of loops. There can be any number of loops inside one another with any of the three combinations depending on the complexity of the given problem. Let us have a look at the different combinations.

1. Nesting of for loop

Syntax:

    for (initialize ; condition ; increment)
    {
	    Statement(s);
	    for (initialize ; condition ; increment)
	    {
		    Statement(s);
		    ...;
	    }
	    ...;
    }

Example: Print number 1 to 10, 5 times

    1 2 3 4 5 6 7 8 9 10 
    1 2 3 4 5 6 7 8 9 10 
    1 2 3 4 5 6 7 8 9 10 
    1 2 3 4 5 6 7 8 9 10 
    1 2 3 4 5 6 7 8 9 10 

C code

#include <stdio.h>

int main()
{
	int i; //for outer loop counter
	int j; //for inner loop counter
	
	for( i=1; i<=5; i++)
	{
		for( j=1; j<=10; j++)
		{
			printf("%d ",j);
		}
		printf("\n");
	}
	
	return 0;
}

2. Nesting of while loop

These loops are mostly used for making various pattern programs in C like number patterns or shape patterns, etc.

Syntax:

    while (condition 1)
    {
	    Statement(s);
	    while (condition 2)
	    {
		    Statement(s);
		    ...;
	    }
	    ...;
    }

Example 1: Print number 1 to 10, 5 times

    1 2 3 4 5 6 7 8 9 10 
    1 2 3 4 5 6 7 8 9 10 
    1 2 3 4 5 6 7 8 9 10 
    1 2 3 4 5 6 7 8 9 10 
    1 2 3 4 5 6 7 8 9 10 

C code

#include <stdio.h>

int main()
{
	int i; //for outer loop counter
	int j; //for inner loop counter

	i=1;
	while( i<=5 )
	{
		j=1;
		while( j<=10 )
		{
			printf("%d ",j);
			j++;
		}
		printf("\n");
		i++;
	}
	
	return 0;
}

Example 2: Printing the following pattern,

    1 
    1 2 
    1 2 3 
    1 2 3 4 
    1 2 3 4 5 

C code

#include <stdio.h>

int main()
{
	int i; //for outer loop counter
	int j; //for inner loop counter

	i=1;
	while( i<=5 )
	{
		j=1;
		while( j<=i )
		{
			printf("%d ",j);
			j++;
		}
		printf("\n");
		i++;
	}
	
	return 0;
}

3. Nesting of do...while loop

Syntax:

    do
    {
	    Statement(s);
	    do
	    {
		    Statement(s);
		    ...;
	    } while (condition 2);
	    ...;
    } while (condition 1);

Example 1: Print number 1 to 10, 5 times

    1 2 3 4 5 6 7 8 9 10 
    1 2 3 4 5 6 7 8 9 10 
    1 2 3 4 5 6 7 8 9 10 
    1 2 3 4 5 6 7 8 9 10 
    1 2 3 4 5 6 7 8 9 10 

C code

#include <stdio.h>

int main()
{
	int i; //for outer loop counter
	int j; //for inner loop counter

	i=1;
	do
	{
		j=1;
		
		do
		{
			printf("%d ",j);
			j++;
		}while( j<=10 );
		
		printf("\n");
		i++;
		
	}while( i<=5 );
	
	return 0;
}

Example 2: Printing the following pattern,

    1 
    1 2 
    1 2 3 
    1 2 3 4 
    1 2 3 4 5 

C code

#include <stdio.h>

int main()
{
	int i; //for outer loop counter
	int j; //for inner loop counter

	i=1;
	do
	{
		j=1;
		
		do
		{
			printf("%d ",j);
			j++;
		}while( j<=i );
		
		printf("\n");
		i++;
	}while( i<=5 );
	
	return 0;
}

Author's note:

These programs are tried and tested in LINUX GCC Compiler. For better understanding of code, try it yourself and run it. Don’t restrict yourself to only these conditions and try other problems from different books or internet.

I'm always there to help in case of problems. Happy coding :)




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.