C++ Nested Loop

Writing a loop inside another loop is known as nested loop. Maximum level of nesting allowed in C++ is 256. We can write while , do...while , for and range based for loop in a nested loop.

Nested While Loop

The syntax of nested while loop is shown below

while (expression) 
{
	while (expression) 
	{
		statement(s)
	}
	statement(s)
}

Below is an example of nested while loop

#include <iostream>
using namespace std;
 
int main ()
{
   int i = 0;
   
   while(i < 3)
   {
       int j = 0;
       while(j < 5)
       {
           cout << "i = " << i << " and j = " << j << endl;
           j++;
       }
       i++;       
   }
   return 0;
}

Below is the output of above program

i = 0 and j = 0
i = 0 and j = 1
i = 0 and j = 2
i = 0 and j = 3
i = 0 and j = 4
i = 1 and j = 0
i = 1 and j = 1
i = 1 and j = 2
i = 1 and j = 3
i = 1 and j = 4
i = 2 and j = 0
i = 2 and j = 1
i = 2 and j = 2
i = 2 and j = 3
i = 2 and j = 4

Nested Do-While Loop

The syntax of nested do…while loop is shown below

do
{
	do
	{
		statement(s)
	}while (expression) 
	
	statement(s)
}while (expression)

Below is an example of nested do…while loop

#include <iostream>
using namespace std;

int main ()
{
   int i = 0;
   
   do
   {
       int j = 0;
       do
       {
           cout << "i = " << i << " and j = " << j << endl;
           j++;
       }while(j < 5);

       i++;       
       
   }while(i < 3);
   
   return 0;
}

Below is the output of above program

i = 0 and j = 0
i = 0 and j = 1
i = 0 and j = 2
i = 0 and j = 3
i = 0 and j = 4
i = 1 and j = 0
i = 1 and j = 1
i = 1 and j = 2
i = 1 and j = 3
i = 1 and j = 4
i = 2 and j = 0
i = 2 and j = 1
i = 2 and j = 2
i = 2 and j = 3
i = 2 and j = 4

Nested For Loop

The syntax of nested for loop is shown below

for (initialization; termination; increment-decrement) 
{
    for (initialization; termination; increment-decrement) 
	{
    	statement(s)
	}
	statement(s)
}

Below is an example of nested for loop

#include <iostream>
using namespace std;
 
int main ()
{
	for(int i = 0;  i < 3; i++)
    {
       int j = 0;
       for(int j = 0;  j < 5; j++)
       {
           cout << "i = " << i << " and j = " << j << endl;           
       }       
    }
   	return 0;
}

Below is the output of above program

i = 0 and j = 0
i = 0 and j = 1
i = 0 and j = 2
i = 0 and j = 3
i = 0 and j = 4
i = 1 and j = 0
i = 1 and j = 1
i = 1 and j = 2
i = 1 and j = 3
i = 1 and j = 4
i = 2 and j = 0
i = 2 and j = 1
i = 2 and j = 2
i = 2 and j = 3
i = 2 and j = 4

Nested Range-Based For Loop

The syntax of nested range-based for loop is shown below

for ( range_declaration : range_expression )
{ 
	for ( range_declaration : range_expression )
	{ 
		statement(s);
	}
	statement(s);
}

Below is an example of nested for loop

#include <iostream>
using namespace std;
 
int main ()
{
    int arr1[3] = {0,1,2};
    int arr2[5] = {0,1,2,3,4};
    for(int i : arr1)
    {
        for(int j : arr2)
        {
            cout << "i = " << i << " and j = " << j << endl;
        }   
    }

   	return 0;
}

Below is the output of above program

i = 0 and j = 0
i = 0 and j = 1
i = 0 and j = 2
i = 0 and j = 3
i = 0 and j = 4
i = 1 and j = 0
i = 1 and j = 1
i = 1 and j = 2
i = 1 and j = 3
i = 1 and j = 4
i = 2 and j = 0
i = 2 and j = 1
i = 2 and j = 2
i = 2 and j = 3
i = 2 and j = 4

 

Translate ยป