What is a Nested Loop?
A nested loop is a loop inside another loop.
Why Nested Loops?
- Used when one repetition depends on another
- Mainly used for pattern printing
- Used in matrices, tables, grids, games
Structure of Nested Loop
for i in range(...):
for j in range(...):
statements
How Nested Loop Works
- Outer loop runs first
- Inner loop runs fully for each outer loop iteration
Pattern Printing Concept
- Outer loop → number of rows
- Inner loop → number of columns
print("*", end=" ")prints in same lineprint()moves to next line
Example: Simple Star Pattern
for i in range(3):
for j in range(3):
print("*", end=" ")
print()
Important Points
- Indentation is very important
- Understand loop flow before memorizing
- Patterns help improve logic
Now attempt the quiz below.
Day 5 Quiz – Nested Loops & Patterns
Select the correct answer and submit the quiz.
0 Comments