Nested Loops In C++ Programming Language

 


Nested  Loops In C++ Programming Language

We have studied about loops in C++ programming language. There are three looping statements that we worked with so far i.e, while loop, do-while loop, and for loop. We know that we use loops when we want to repeat or iterate some piece of code according to our programming logic and requirement. In this blog post we are going to look at nested loops in C++ programming language. Now the first question arises that what is a nested loop. At this point so far we already have knowledge about loops. Nested loops are nothing they are just a loop that is placed or resides completely inside another loop. In simple words it is a loop inside another loop. And remember one important point, the inner loop completes all of its iterations every single time the outer loop runs once.



Basic Syntax Of Nested Loops In C++ Programming Language


Syntax of for loop in C++ programming language

for (initialization; condition; increment)
{
	for (initialization; condition; increment)
	{
		// write code logic here.
	}
}

Basic working of for loop in C++ programming language

As you can see in the above nested for loop syntax, we have simply placed a for loop inside another for loop. Remember an important point for every outer for loop iteration the inner for loop will iterate the complete looping cycles. For example if the outer and inner loop both will run 3 times, then for the first iteration of outer for loop, the inner for loop will complete it's three iterations. And when the second outer iteration will run, then again the inner for loop will run again 3 times. Which means that when the outer for loop will complete its three iterations, the inner for loop will have completed its 9 iterations. If your are not getting this point right now don't worry we are going to see some examples at that time all your queries will be satisfied and you will understand the concept completely.


Syntax of while loop in C++ programming language

int i = 1;
while (condition)
{
	int j = 1;
	while (condition)
	{
		// write code logic here.
		// increment some variable j++.
	}
	// increment some variable i++.
}



Basic working of while loop in C++ programming language

Now we are going to look at the nested while loop syntax and it's working. The main logic for the nested while loop is exactly the same as that of the for loop. Which means that for every single iteration of outer while loop, the inner while loop will complete its all the iterations as per the condition of inner while loop. In simple words if both outer and inner while loops will have conditions to run three times. Then for each single iteration of outer while loop, the inner while loop will complete all it's three iterations.


Syntax of do-while loop in C++ programming language
int i = 1;
do
{
	int j = 1;
	do
	{
		// write code logic here.
		// increment some variable j++.
	} while (condition);
	// increment some variable i++.
} while (condition);


Basic working of while loop in C++ programming language

Now we will cover syntax of nested do-while loop in C++ programming language. The syntax of nested do while loop is almost similar to the nested while and also the logic of all of the three nested loops will work in exactly the same manner. Now let us jump to the working example of nested loops so that all questions and queries will satisfied and fully answered.


Basic Working Of Nested for Loops In C++ Programming Language

coding syntax of for loop

for (int i = 1; i <= 3; i++)
{
	for (int j = 1; j <= 3; j++)
	{
		cout << i << " " << j << endl;
	}
}



Working of above nested for loop coding example 


In the above line of code we are looking at a nested for loop. Now if we simply place another loop inside this for loop, that loop is called nested loop. Remember we can nest or put any loop inside this for loop i.e, we can put for loop inside another for loop or while loop inside a for loop or do-while loop inside a for loop. And the same is ture for other loops like while and do-while loops.

When we place a for loop inside another for loop, remember one thing that when the outer for loop will run one time the inner for loop will complete all of its iterations and after that the outer for loop will run or iterate second time. If at this point it is difficult to understand this, it is not a problem we will cover some examples which will make all the points clear.


Simple Programming Examples Of Nested Loops In C++ Programming Language

 Now we will look at three simple coding examples 1) for loop, 2) while loop, 3) do-while loop.


Coding example 1 nested for loop

#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
	for (int i = 1; i <= 3; i++)
	{
		for (int j = 1; j <= 3; j++)
		{
			cout << i << " " << j << endl;
		}
	}
	_getch();
}

Explanation of code

In the above coding example we have used the nested for loop. It means a for loop is nested inside another for loop. In this example a for loop is nested inside another for loop. Now remember we have an outer for loop and an inner for loop. Both the loops will iterate 3 times, now when the outer for loop will run first time the control goes to inner for loop, but here is an interesting thing to remember, the inner loop will run three times when all the three iterations of inner for loop will complete then the control goes to outer for loop, now the outer for loop will iterate second time and again this time the control goes to inner for loop which again iterate three times and after that the control again shifts to outer for loop. Now the outer loop will run or iterate third time and again the inner for loop will iterate three times. After this the control shifts to the outer for loop and according to the given outer for loop condition the for loop exits because the condition is false i.e, i <= 3. So this is how the nested for loop will run and executed successfully.

In the next two coding examples we will see the while loop and the do-while loop. But the logic remains the same in these examples and so the working of nested loops.


Coding example 2 nested while loop

#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
	int i = 1;
	while (i <= 3)
	{
		int j = 1;
		while (j <= 3)
		{
			cout << i << " " << j << endl;
			j++;
		}
		i++;
	}
	_getch();
}


Coding example 3 nested do-while loop

#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
	int i = 1;
	do
	{
		int j = 1;
		do
		{
			cout << i << " " << j << endl;
			j++;
		} while (j <= 3);
		i++;
	} while (i <= 3);
	_getch();
}


Congratulations! like always we have successfully implemented the nested loops in C++ programming language.


Comments

Popular posts from this blog

Install Visual Studio Community 2022 for Free (2026 Guide)

For Loop In C++ Programming Language

Your First C Program: How to Write a Hello World Program in C

Functions In C++ Programming Language