Here is a complete explanation of Nested Loops with real-life examples, Python code, step-by-
step execution, and break and continue. This is suitable for beginners and exam preparation.
Nested Loops in Python
Definition
A nested loop is a loop inside another loop.
The outer loop controls how many times the whole process repeats.
The inner loop performs a repeated task inside each repetition of the outer loop.
Rule:
For every one iteration of the outer loop, the inner loop completes all of its iterations.
How Nested Loops Work
Suppose we have:
for i in range(3):
for j in range(2):
print(i, j)
Step-by-step
Outer Loop (i = 0)
Inner loop starts.
0 0
0 1
Inner loop finishes.
Outer Loop (i = 1)
Inner loop starts again.
1 0
1 1
Inner loop finishes.
Outer Loop (i = 2)
Inner loop starts again.
2 0
2 1
Inner loop finishes.
Outer loop ends.
Output
0 0
0 1
1 0
1 1
2 0
2 1
Notice something:
The inner loop starts from the beginning every time.
Real-Life Example 1: School Attendance
Imagine a teacher checks attendance.
There are:
3 Classes
Each class has 4 students
Teacher first enters Class 1.
Checks
Student 1
Student 2
Student 3
Student 4
Then goes to Class 2.
Again checks
Student 1
Student 2
Student 3
Student 4
Then Class 3.
Again checks
Student 1
Student 2
Student 3
Student 4
Here
Outer loop = Classes
Inner loop = Students
Python Code
for classroom in range(1,4):
print("Class", classroom)
for student in range(1,5):
print(" Student", student)
Output
Class 1
Student 1
Student 2
Student 3
Student 4
Class 2
Student 1
Student 2
Student 3
Student 4
Class 3
Student 1
Student 2
Student 3
Student 4
Real-Life Example 2: Pizza Shop
Suppose a customer orders
3 pizzas
Every pizza gets
Cheese
Chicken
Olives
The chef finishes one pizza before making the next.
Outer loop
Pizza number
Inner loop
Adding toppings
Python Code
toppings = ["Cheese","Chicken","Olives"]
for pizza in range(1,4):
print("Making Pizza", pizza)
for topping in toppings:
print(" Adding", topping)
print(" Pizza Ready\n")
Output
Making Pizza 1
Adding Cheese
Adding Chicken
Adding Olives
Pizza Ready
Making Pizza 2
Adding Cheese
Adding Chicken
Adding Olives
Pizza Ready
Making Pizza 3
Adding Cheese
Adding Chicken
Adding Olives
Pizza Ready
Nested while Loop in Python
Definition
A nested while loop is a while loop inside another while loop.
The outer while loop controls the main repetition.
The inner while loop performs a repeated task and completes all its iterations for each
iteration of the outer loop.
Exam Definition
A nested while loop is a while loop placed inside another while loop. For every iteration of
the outer loop, the inner loop executes completely until its condition becomes False.
Simple Explanation
Think of it like this:
🔵 Outer while loop = Repeat the whole process.
🟢 Inner while loop = Complete all the work inside each repetition.
Rule:
The inner loop always finishes completely before the outer loop starts its next iteration.
Syntax
while outer_condition:
# Outer loop code
while inner_condition:
# Inner loop code
How It Works
Outer Loop Starts
│
▼
Inner Loop Starts
│
Runs until condition becomes False
│
▼
Inner Loop Ends
│
▼
Outer Loop Moves to Next Iteration
Example 1: Basic Nested while Loop
i = 1
while i <= 3: # Outer loop
j = 1
while j <= 2: # Inner loop
print(i, j)
j += 1
i += 1
Nested while Loop
room = 1
while room <= 3:
print("Room", room)
patient = 1
while patient <= 2:
print(" Patient", patient)
patient += 1
room += 1
Output
Room 1
Patient 1
Patient 2
Room 2
Patient 1
Patient 2
Room 3
Patient 1
Patient 2
Mixing for and while
for class_no in range(1,4):
student = 1
while student <= 3:
print("Class", class_no,
"Student", student)
student += 1
break in Nested Loops
Rule
break stops only the loop in which it appears.
It usually stops the inner loop.
Real-Life Example
Suppose a teacher is checking students.
Student 3 becomes sick.
Teacher stops checking that class.
But continues with the next class.
Code
for classroom in range(1,4):
print("Class", classroom)
for student in range(1,6):
if student == 3:
print(" Sick student found")
break
print(" Student", student)
Output
Class 1
Student 1
Student 2
Sick student found
Class 2
Student 1
Student 2
Sick student found
Class 3
Student 1
Student 2
Sick student found
Notice
Teacher did not check Student 4 and Student 5.
Because break stopped the inner loop.
But the teacher still moved to the next class.
continue in Nested Loops
Rule
continue skips only one iteration of the loop in which it appears.
The loop keeps running afterward.
Real-Life Example
Teacher checks attendance.
Student 3 is absent.
Teacher skips Student 3.
Continues checking Student 4 and Student 5.
Code
for classroom in range(1,3):
print("Class", classroom)
for student in range(1,6):
if student == 3:
print(" Student absent")
continue
print(" Student", student)
Output
Class 1
Student 1
Student 2
Student absent
Student 4
Student 5
Class 2
Student 1
Student 2
Student absent
Student 4
Student 5
Notice
Student 3 is skipped.
Students 4 and 5 are still checked.
Another continue Example
Pizza Shop
Customer does not want Olives.
Skip Olives.
Still add the remaining toppings.
toppings = ["Cheese","Olives","Chicken","Sauce"]
for pizza in range(1,3):
print("Pizza", pizza)
for topping in toppings:
if topping == "Olives":
continue
print(" Add", topping)
Output
Pizza 1
Add Cheese
Add Chicken
Add Sauce
Pizza 2
Add Cheese
Add Chicken
Add Sauce
Where Nested Loops Are Used
Pattern printing (*, numbers, triangles)
Multiplication tables
Chess boards
Seating arrangements
Attendance systems
Libraries (shelves and books)
Hotels (floors and rooms)
Hospitals (wards and patients)
Schools (classes and students)
Shopping carts (customers and items)
Matrices (2D arrays)
Games (rows and columns)
Difference Between break and continue in Nested Loops
Feature break continue
Stops loop? Yes No
Skips one iteration? No Yes
Affects outer loop? No No
Affects inner loop? Yes (stops it) Yes (skips current iteration only)
Execution after statement Exits current loop Continues with next iteration
Exam Summary
Nested loop: A loop inside another loop.
Outer loop: Controls how many times the whole process repeats.
Inner loop: Completes all of its iterations for each iteration of the outer loop.
Nested for: Best when the number of iterations is known.
Nested while: Best when repetition depends on a condition.
break: Stops only the loop in which it appears (usually the inner loop).
continue: Skips only the current iteration of the loop in which it appears, then continues
with the next iteration.
Common uses: Grids, tables, patterns, matrices, attendance systems, seating plans, and
many other real-world situations.