Updated March 27, 2023
Introduction to Nested Loop in C++
A nested loop is a loop in which one loop resides inside another loop where the inner loop gets executed first, satisfying all the set of conditions that prevailed within the loop followed by an outer loop set of conditions. Execution of statements within the loop flows in a way that the inner loop of the nested loop gets declared, initialized and then incremented. Once all the condition within the inner loop gets satisfied and becomes true, it moves for the search of the outer loop. It is often called a “loop within a loop”.
Syntax of Nested Loop in C++
Following are the syntax:
1. Syntax of Nested for Loop
for (initialization; condition; increment) {
for (initialization; condition; increment) {
// set of statements for inner loop
}
//set of statement for outer loop
}
2. Syntax of Nested While loop
while(condition) {
while(condition) {
// set of statement of inside while loop
}
//set of statement for outer while loop
}
3. Syntax of Nested Do-While loop
do {
while(condition) {
for (initialization; condition; increment) {
//set of statement of inside do-while loop
}
// set of statement of inside do-while loop
}
//set of statement of outer do-while loop
} while(condition);
Explanation:
In any loop, the first execution will happen for the set of statements of the inner loop. If the condition gets satisfied and is true, it will again come inside and satisfy the second condition. If that gets false, it will search for the outer loop and try to satisfy all the conditions. In this way, the flow continues. Also, it is not mandatory that the loop must be placed within the loop of the same genre. It can be any loop which means any loop can be placed inside any loop.
Flowchart
Below is the flow chart of a different nested loop:
1. Flowchart of Nested While Loop
Explanation:
Initially, one condition statement is being provided in the while loop; if that condition of inner loop condition statement is true, then loop execution will continue with the same inner loop condition forming a loop as soon as it finds that the condition is false it will come out of the inner while loop and search for the outer loop condition. If the outer loop condition comes out to be true, then it will execute all the sets of statements and information. After following all set of instructions, it will become false, and the loop will come out to the main program control saying end while loop.
2. Nested Do While Loop
Explanation:
A statement is provided as an input followed by a condition which checks whether it satisfies the condition or not; if it satisfies the condition, then again looping will happen; if not, it will come out of the loop and will check for a false condition then while loop gets end then again one more loop gets into execution which will also follow the similar pattern of execution.
3. For Loop
Explanation:
A for loop includes the initialization of variable followed by condition 1; if condition 1 gets satisfied, it will search for the second condition if that also gets true, it will get incremented and then when the condition satisfies, and it gets true, then it searches for the new set of statements, following the same flow of execution. Once the condition becomes false, it comes out of the loop and searches for an end of for statement, reaching back to the entire program execution’s main condition.
How Nested Loop works in C++?
Looping plays a very pivotal role in any programming language; the same it happens in the case of C++. When one loop resides inside another loop is called nesting. When we loop two loops together, i.e. kind of nesting, then the outer loop takes control of the number of times the inner loop works and takes care of all manipulation and computation.
Examples of Nested Loop in C++
Given below are the examples of Nested Loop in C++:
Example #1
Nested loop with a while loop for getting all the numbers from 1 – 20.
Code:
#include <iostream>
int main () {
using namespace std;
int a = 1;
while (a <= 20) {
cout << a << endl;
a++;
}
return 0;
}
Output:
Example #2
Program with a while nested loop to verify whether the number is odd or even.
Code:
#include <iostream>
int main () {
using namespace std;
int choose = 1;
while (choose == 1) {
int b;
cout << "Enter a number to verify odd or even" << endl;
cin >> b;
if (b%2 == 0) {
cout << " Number is even" << endl;
}
else {
cout << " Number is odd" << endl;
}
cout << "To check for more: 1 for yes and 0 for no" << endl;
cin >> choose;
}
cout << "All numbers are verified" << endl;
return 0;
}
Output:
Example #3
Program with a nested for loop to calculate the sum of given numbers within the specified range.
Code:
#include <iostream>
int main () {
using namespace std;
int sum = 0, c, d;
for (c = 0; c < 10; c++) {
cout << "Enter the number" << endl;
cin >> d;
sum = sum + d;
}
cout << "Sum is " << sum << endl;
return 0;
}
Output:
Example #4
Program to print the numbers using a do-while loop.
Code:
# include<iostream>
int main () {
using namespace std;
int k = 1;
do {
cout << k << endl;
k++;
} while (k <= 15);
return 0;
}
Output:
Example #5
Program to get the multiplication table of a given number in the m*n format using nested for loop.
Code:
#include <iostream>
int main () {
using namespace std;
int m;
int n;
for (m = 10; m <= 11; m++) {
cout << "Table of " << m << endl;
for (n = 1; n <= 10; n++) {
cout << m << "*" << n << "=" << (m*n) << endl;
}
}
return 0;
}
Output:
Conclusion
Thus, nesting in any programming language revolves around the same concept: a loop inside the loop. Control of the inner loop is taken care of by an outer loop which can also be provided with a range, and it is not mandatory to put the looping under some format of the defined block. Nesting can be done within any block of code, for nesting loop or while nesting loop both takes care of statement flow based on conditions satisfaction.
Recommended Articles
This is a guide to Nested Loop in C++. Here we discuss the introduction, various examples, and how nested loop works in C++? You may also have a look at the following articles to learn more –