Nested Loops

1. What are Nested Loops?

<aside> <img src="/icons/orbit_gray.svg" alt="/icons/orbit_gray.svg" width="40px" />

Formal Definition

A nested loop is a loop that is placed inside the body of another loop.

The loop that contains another loop is called the outer loop, and the loop inside it is called the inner loop.

</aside>

In simple words:

When one loop runs completely inside another loop, it is called a nested loop.

Key Concept

Real-World Analogy

Analogy 1: School Timetable

For each day:
    For each period:
        Teach a subject

Each period finishes completely before moving to the next day.

Analogy 2: Rows and Columns in a Classroom

To count benches: