Nested Loops in C++
A nested loop is simply a loop inside another loop. It’s the tool you reach for whenever you’re working with rows and columns — grids, tables, and the star patterns that show up in every beginner exercise set. Once you see how the two loops interact, all of those problems start to look the same.
How a Nested Loop Works
The outer loop controls the rows; the inner loop runs fully for each single step of the outer loop. Watch the order things print:
#include <iostream>
int main() {
for (int row = 1; row <= 3; ++row) {
for (int col = 1; col <= 4; ++col) {
std::cout << "(" << row << "," << col << ") ";
}
std::cout << "\n";
}
return 0;
}
For row = 1, the inner loop runs all the way from col = 1 to col = 4. Only then does row become 2 and the inner loop starts over. The "\n" after the inner loop is what moves you to the next row.
Example 1: A Rectangle of Stars
Print stars in a fixed grid — outer loop for rows, inner loop for the stars across each row:
#include <iostream>
int main() {
int rows = 4, cols = 6;
for (int r = 0; r < rows; ++r) {
for (int c = 0; c < cols; ++c) {
std::cout << "* ";
}
std::cout << "\n";
}
return 0;
}
This gives you a clean 4 by 6 block of stars. Change rows and cols to resize it.
Example 2: A Right-Triangle Pattern
Here’s where nested loops get interesting. Make the inner loop’s limit depend on the outer counter, and the shape changes:
#include <iostream>
int main() {
int height = 5;
for (int r = 1; r <= height; ++r) {
for (int c = 1; c <= r; ++c) {
std::cout << "*";
}
std::cout << "\n";
}
return 0;
}
Row 1 prints 1 star, row 2 prints 2, and so on, because the inner loop runs r times. That single link — c <= r — turns a rectangle into a triangle. Almost every pattern puzzle is a variation of this idea.
Example 3: The Multiplication Table
A times table is just a grid where each cell holds row * column. Using std::setw lines the numbers up in neat columns:
#include <iostream>
#include <iomanip>
int main() {
for (int i = 1; i <= 10; ++i) {
for (int j = 1; j <= 10; ++j) {
std::cout << std::setw(4) << i * j;
}
std::cout << "\n";
}
return 0;
}
The outer loop picks the row number, the inner loop walks across the columns, and i * j fills in each product. std::setw(4) reserves four spaces per number so the grid stays aligned.
Watch the Iteration Count
Nested loops multiply work. Two loops of 10 means 100 inner steps; two loops of 1,000 means a million. That’s fine for printing a small table, but it’s worth remembering: as the numbers grow, nested loops grow much faster. When a problem feels slow, a hidden pair of nested loops is often the reason.
A quick tip: give your counters different names (i/j or row/col). Reusing the same variable for both loops is a classic beginner bug that breaks the inner loop’s count.
Related Articles
- C++ Loops Tutorial — for, while, and do-while basics
- C++ break and continue — control the flow inside loops
- C++ 2D Arrays — store the grids your loops walk over
- C++ Arrays Tutorial — the foundation for rows and columns
- C++ Conditionals Tutorial — add logic inside your loops
Take Your C++ Further
If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.