1. Intro
A nested loop is simply a loop written inside the body of another loop. The outer loop controls how many times the entire inner loop runs, and the inner loop runs to completion for every single iteration of the outer loop. Nested loops are the standard tool for working with two-dimensional data, such as grids, matrices, and printed patterns like triangles or number tables.
Cricket analogy: Printing a full tournament's over-by-over scoresheet needs an outer loop for each match and an inner loop for each over within that match — exactly the nested-loop structure used for grids like a Duckworth-Lewis table.
2. Syntax
for (int i = 0; i < outerLimit; i++) {
for (int j = 0; j < innerLimit; j++) {
// inner loop body
}
}3. Explanation
Any loop type (for, while, do-while) can be nested inside any other loop type. The total number of times the innermost body executes is the product of the outer loop's iteration count and the inner loop's iteration count (outerLimit x innerLimit for two simple counted loops). Each time the outer loop advances by one iteration, the inner loop's counter is reset and the inner loop runs through its full range again from the start. This is a common exam trap: students forget that the inner loop restarts completely on every pass of the outer loop, rather than continuing from where it left off.
Cricket analogy: Every one of a bowler's 10 overs (outer) runs a fresh set of 6 balls (inner) — the ball counter resets to 1 at the start of each new over rather than continuing from the last over's count, a common scoring mix-up mirroring the nested-loop reset trap.
4. Example
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
cout << "i=" << i << " j=" << j << " ";
}
cout << endl;
}
return 0;
}5. Output
i=1 j=1 i=1 j=2
i=2 j=1 i=2 j=2
i=3 j=1 i=3 j=2 6. Key Takeaways
- A nested loop is a loop placed inside the body of another loop.
- The inner loop fully restarts and runs to completion for every single iteration of the outer loop.
- Total inner-body executions equal outer iterations multiplied by inner iterations (e.g. 3 x 2 = 6 above).
- Nested loops are the standard way to process 2D structures like grids, matrices, and printed patterns.
Practice what you learned
1. In a nested loop with the outer loop running 4 times and the inner loop running 5 times each pass, how many total times does the inner loop's body execute?
2. What happens to the inner loop's counter when the outer loop begins a new iteration?
3. For `for (int i = 1; i <= 2; i++) { for (int j = 1; j <= 3; j++) cout << j; }`, what is printed?
4. Which C++ feature is typically used with nested loops to print a 2D pattern, like a multiplication table?
5. Can different loop types (e.g. `for` inside `while`) be nested together in C++?
Was this page helpful?
You May Also Like
for Loop in C++
Learn the C++ `for` loop syntax, how init/condition/increment control iteration, and when it is the best choice for a known number of repetitions.
Multidimensional Arrays in C++
Understand multidimensional arrays in C++ as arrays of arrays, how 2D arrays model rows and columns, and how row-major storage works.
while Loop in C++
Understand the C++ `while` loop, how its entry condition is tested before every iteration, and when to use it over a `for` loop.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics