C++ Nested for Loops
In this page:
for (int outer = start; outer < limit; outer++) {
for (int inner = start; inner < limit; inner++) {
// inner body
}
}
What is a Nested for Loop?
A nested for loop places one for loop entirely inside the body of another, and the inner loop runs through its full range once for every single iteration of the outer loop.
Example: What is a Nested for Loop?
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Loop: repeat while the condition holds
for (int i = 1; i <= 2; i++) {
// Loop: repeat while the condition holds
for (int j = 1; j <= 3; j++) {
// Print to the console with cout
std::cout << i << "-" << j << " ";
}
}
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Printing a Grid Pattern
Nested loops are the standard technique for printing two-dimensional patterns like grids or triangles, with the outer loop typically controlling rows and the inner loop controlling columns within each row.
Example: Printing a Grid Pattern
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Loop: repeat while the condition holds
for (int row = 1; row <= 3; row++) {
// Loop: repeat while the condition holds
for (int col = 1; col <= 3; col++) {
// Print to the console with cout
std::cout << "* ";
}
// Print to the console with cout
std::cout << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Nested Loops over a 2D Array
Iterating over a two-dimensional array almost always uses nested loops, with the outer loop's index selecting a row and the inner loop's index selecting a column within that row.
Example: Nested Loops over a 2D Array
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
// Loop: repeat while the condition holds
for (int row = 0; row < 2; row++) {
// Loop: repeat while the condition holds
for (int col = 0; col < 3; col++) {
// Print to the console with cout
std::cout << grid[row][col] << " ";
}
}
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Nested Loops with Different Bounds
The outer and inner loops in a nested pair don't need matching bounds, and the inner loop's bound can even depend on the outer loop's current variable, which is how growing or shrinking patterns are produced.
Example: Nested Loops with Different Bounds
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Loop: repeat while the condition holds
for (int i = 1; i <= 3; i++) {
// Loop: repeat while the condition holds
for (int j = 1; j <= i; j++) {
// Print to the console with cout
std::cout << "* ";
}
// Print to the console with cout
std::cout << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Performance of Nested Loops
The total number of iterations in a nested loop is the product of the outer and inner loop counts, so nested loops over large ranges can quickly become expensive, an important consideration when reasoning about performance.
Example: Performance of Nested Loops
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare outerCount and set it to 100
int outerCount = 100;
// Declare innerCount and set it to 100
int innerCount = 100;
long totalIterations = (long)outerCount * innerCount;
// Print to the console with cout
std::cout << "Total iterations: " << totalIterations << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Reusing the same variable such as
iin both the inner and outer loop, which breaks the counting. - Getting bounds wrong so the inner loop indexes past the array end.
- Not noticing that the inner body runs outer times inner times, which can make programs very slow.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: