Nested Loops in C++ with Examples Last Updated : 03 Dec, 2019 Comments Improve Suggest changes Like Article Like Report Nested loop means a loop statement inside another loop statement. That is why nested loops are also called as "loop inside loop". Syntax for Nested For loop: for ( initialization; condition; increment ) { for ( initialization; condition; increment ) { // statement of inside loop } // statement of outer loop } Syntax for Nested While loop: while(condition) { while(condition) { // statement of inside loop } // statement of outer loop } Syntax for Nested Do-While loop: do{ do{ // statement of inside loop }while(condition); // statement of outer loop }while(condition); Note: There is no rule that a loop must be nested inside its own type. In fact, there can be any type of loop nested inside any type and to any level. Syntax: do{ while(condition) { for ( initialization; condition; increment ) { // statement of inside for loop } // statement of inside while loop } // statement of outer do-while loop }while(condition); Below are some examples to demonstrate the use of Nested Loops: Example 1: Below program uses a nested for loop to print a 2D matrix of 3x3. CPP // C++ program that uses nested for loop // to print a 2D matrix #include <bits/stdc++.h> using namespace std; #define ROW 3 #define COL 3 // Driver program int main() { int i, j; // Declare the matrix int matrix[ROW][COL] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; cout << "Given matrix is \n"; // Print the matrix using nested loops for (i = 0; i < ROW; i++) { for (j = 0; j < COL; j++) cout << matrix[i][j]; cout << "\n"; } return 0; } Output: Given matrix is 123 456 789 Example 2: Below program uses a nested for loop to print all prime factors of a number. CPP // C++ Program to print all prime factors // of a number using nested loop #include <bits/stdc++.h> using namespace std; // A function to print all prime factors of a given number n void primeFactors(int n) { // Print the number of 2s that divide n while (n % 2 == 0) { cout << 2; n = n / 2; } // n must be odd at this point. So we can skip // one element (Note i = i +2) for (int i = 3; i <= sqrt(n); i = i + 2) { // While i divides n, print i and divide n while (n % i == 0) { cout << i; n = n / i; } } // This condition is to handle the case when n // is a prime number greater than 2 if (n > 2) cout << n; } /* Driver program to test above function */ int main() { int n = 315; primeFactors(n); return 0; } Output: 3357 Comment More infoAdvertise with us Next Article Nested Loops in C++ with Examples C code_r Follow Improve Article Tags : C++ Loops & Control Structure Practice Tags : CPP Similar Reads Array of list in C++ with Examples What is an array? An array in any programming language is a data structure that is used to store elements or data items of similar data types at contiguous memory locations and elements can be accessed randomly using indices of an array. Arrays are efficient when we want to store a large number of e 5 min read Vector of Vectors in C++ STL with Examples In C++, a vector of Vectors is a two-dimensional vector with a variable number of rows, where each row is a vector. Each index of a vector stores a vector that can be traversed and accessed using iterators. It is similar to an Array of Vectors but with dynamic properties. Syntax:C++vector<vector 4 min read fill() function in C++ STL with examples The fill() function in C++ STL is used to fill some default value in a container. The fill() function can also be used to fill values in a range in the container. It accepts two iterators begin and end and fills a value in the container starting from position pointed by begin and just before the pos 2 min read Nested list in C++ STL list in STL is used to represent a linked list in C++. How to create a nested list. We are given n lists, we need to create a list of n lists. Examples: Input : Number of lists: 2 1st list: {1 2} 2nd list: {3 4 5 6} Output : [ [ 1 2 ] [ 3 4 5 6 ] ] Input : Number of lists: 3 1st list : {0 1} 2nd lis 2 min read Writing First C++ Program - Hello World Example The "Hello World" program is the first step towards learning any programming language and is also one of the most straightforward programs you will learn. It is the basic program that demonstrates the working of the coding process. All you have to do is display the message "Hello World" on the outpu 4 min read Like