Nested loops are a control structure that consists of placing loops inside other loops.
Here, loop can be any of the ones we’ve seen WHILE, DOWHILE, FOR, FOREACH, depending on what our language provides.
Nested loops are especially useful (and necessary) when iterating over complex data structures or when repetitive operations are needed at different hierarchical levels.
By placing a loop inside another loop, we get a structure like the following:

- The outer loop is the loop that encloses other loops.
- Inside it, we define the inner loop.
Translated into code, it would look like this:
loop(...) // outer loop 🔵
{
// Actions that occur before the inner loop
loop(...) // inner loop 🟨
{
// Action that repeats in the inner loop
}
// Action that occurs after the outer loop
}
When the code executes:
- First, the outer loop 🔵 executes.
- The lines before the inner loop 🔵 execute.
- Upon reaching the inner loop 🟨, the code gets “stuck there going in circles” 🔁.
- When the inner loop finishes, the remaining lines of the outer loop 🔵 execute.
- Upon reaching the end of the outer loop, it repeats 🔁.
Examples of Nested Conditionals
Let’s see an example in different programming languages of how to create nested loops:
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
// Action that repeats in the inner loop
}
// Action that repeats in the outer loop
}
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
// Action that repeats in the inner loop
}
// Action that repeats in the outer loop
}
for (let i = 0; i < 5; i++)
{
for (let j = 0; j < 3; j++)
{
// Action that repeats in the inner loop
}
// Action that repeats in the outer loop
}
for i in range(5):
for j in range(3):
# Action that repeats in the inner loop
# Action that repeats in the outer loop
In these examples, we’ve created a FOR type loop with:
- 5 repetitions of the outer loop
- 3 repetitions of the inner loop
This means that, in total:
- The outer loop repeats 5 times
- The inner loop repeats 5 x 3 = 15 times.
Best Practices Tips
Nested loops are common and necessary to solve complex problems (especially with structured and grouped data).
For example, imagine the following code, which simulates evaluating all students in a class.
foreach(classroom)
{
foreach(student in classroom)
{
evaluate_student(student)
}
}
If I need to perform that calculation for all classroomss and all studentss in each of them, a nested loop is the simplest solution.
However, nested loops also have their problems. The main one is that the number of loop executions increases exponentially.
If, for example, you have a loop that executes 10,000 times, inside a loop that executes 10,000 times, the inner loop will execute 100 million times!
for(10000)
{
for(10000)
{
// this will execute 100,000,000 times!!
}
}
Now imagine if you put another loop inside… ⌛⌛⌛ … So, don’t get too carried away putting loops inside loops.
Another problem is that in WHILE type loops, it can complicate the exit condition. It’s easier to get into an infinite loop.
The last one is that it can be code that’s harder to understand. For example, the previous example might be easier to understand if we “split” the evaluate_student and evaluate_classroom functions.
foreach(classroom)
{
evaluate_classroom(classroom)
}
function evaluate_classroom(classroom)
{
foreach(student in classroom)
{
evaluate_student(student)
}
}
As always, it’s important to maintain a clear and readable code structure, so it’s easy to understand for you or for the next person who reads it.
