Understanding Loops: How to Make Your Code Do Repetitive Work Effortlessly
Imagine you had to print the numbers from 1 to 100. Would you write print(1), print(2), print(3)... all the way to print(100)? That would be 100 lines of nearly identical code. There has to be a better way — and there is. It is called a loop.
What Is a Loop?
A loop is a programming construct that repeats a block of code multiple times, either a fixed number of times or until a certain condition is met.
Real-world analogy:
You are washing dishes.
WHILE there are still dirty dishes:
Pick up one dish
Wash it
Put it on the rack
END (when all dishes are clean, you stop)
That "while there are still dishes" logic is exactly how a programming loop works.
Why Loops Matter
Without loops:
print("Good morning, Ananya!")
print("Good morning, Ananya!")
print("Good morning, Ananya!")
# ... repeated 50 times
With a loop:
for i in range(50):
print("Good morning, Ananya!")
One block of code. Fifty executions. This is the power of loops.
The Three Types of Loops
LOOPS
├── for loop → Repeat a fixed number of times
├── while loop → Repeat as long as a condition is true
└── do-while loop → Run at least once, then check the condition
1. The FOR Loop
A for loop is used when you know exactly how many times you want to repeat something.
Syntax (Python):
for variable in range(start, stop):
# code to repeat
Example:
for number in range(1, 6):
print(number)
Output:
1
2
3
4
5
How it works — flowchart:
START
│
▼
Set number = 1
│
▼
┌─────────────────────────┐
│ Is number less than 6? │
└─────────────────────────┘
│ │
YES NO
│ │
▼ ▼
Print number END
│
▼
number = number + 1
│
└──────────► (go back to condition check)
Iterating Over a List
for loops are perfect for going through every item in a list:
fruits = ["mango", "banana", "papaya", "guava"]
for fruit in fruits:
print("I like " + fruit)
Output:
I like mango
I like banana
I like papaya
I like guava
2. The WHILE Loop
A while loop keeps running as long as a condition remains True. You use it when you do not know in advance how many times you will need to repeat.
Syntax:
while condition:
# code to repeat
Example:
attempts = 0
while attempts < 3:
print("Attempt number: " + str(attempts + 1))
attempts = attempts + 1
Output:
Attempt number: 1
Attempt number: 2
Attempt number: 3
How it works — flowchart:
START
│
▼
attempts = 0
│
▼
┌──────────────────────┐
│ Is attempts < 3? │
└──────────────────────┘
│ │
YES NO
│ │
▼ ▼
Print attempt END
│
▼
attempts = attempts + 1
│
└──────────► (go back to condition check)
Infinite Loops — The Bug You Must Avoid
If the condition in a while loop never becomes False, it runs forever. This is called an infinite loop and is one of the most common beginner mistakes.
# DANGER: This loop never ends!
count = 1
while count > 0:
print(count)
count = count + 1 # count keeps growing, never reaches 0
Always make sure the condition will eventually become False.
3. The DO-WHILE Loop
A do-while loop always runs the block of code at least once — it checks the condition after the first execution. Python does not have a built-in do-while, but it exists in JavaScript, Java, and C.
JavaScript example:
let count = 0;
do {
console.log("Count is: " + count);
count++;
} while (count < 3);
Output:
Count is: 0
Count is: 1
Count is: 2
Key difference:
FOR / WHILE DO-WHILE
───────────────────── ─────────────────────
Check condition FIRST Execute FIRST
Then run the block Then check condition
May run 0 times Runs at least 1 time
Loop Control: break and continue
Sometimes you need more control over a running loop.
break — Exit the loop immediately
for number in range(1, 10):
if number == 5:
break
print(number)
Output:
1
2
3
4
The loop stops the moment number equals 5.
continue — Skip this iteration and go to the next
for number in range(1, 8):
if number == 4:
continue
print(number)
Output:
1
2
3
5
6
7
The number 4 is skipped but the loop continues.
Nested Loops — A Loop Inside a Loop
You can place one loop inside another. Each time the outer loop runs once, the inner loop runs completely.
for row in range(1, 4):
for column in range(1, 4):
print(row, column)
Output:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
Visualised as a grid:
Outer loop (row) → 1, 2, 3
Inner loop (col) → for each row: 1, 2, 3
col: 1 2 3
row 1: [11][12][13]
row 2: [21][22][23]
row 3: [31][32][33]
Choosing the Right Loop
SCENARIO BEST LOOP
───────────────────────────────────── ──────────
Repeat exactly N times for
Go through every item in a list for
Keep going until user enters "quit" while
Repeat until a calculation converges while
Show a menu at least once do-while
A Complete Example: Multiplication Table
number = 7
print("Multiplication table of " + str(number))
print("─" * 25)
for i in range(1, 11):
result = number * i
print(str(number) + " x " + str(i) + " = " + str(result))
Output:
Multiplication table of 7
─────────────────────────
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
Summary
| Loop Type | When to use | Runs at least once? |
|------------|--------------------------------------|---------------------|
| for | Known number of repetitions | Not guaranteed |
| while | Unknown repetitions, condition-based | Not guaranteed |
| do-while | Must run at least once | Yes, always |
- Use
breakto exit a loop early - Use
continueto skip one iteration - Always ensure
whileloops have a path toFalse— avoid infinite loops - Nested loops create multi-dimensional repetition
Loops are everywhere in real software — rendering a list on a webpage, checking every row in a database, sending emails to every user. Master them early and you will have one of programming's most useful tools in your hands.
Published by Cypher Async — Agartala's offline coding school, building real engineers one concept at a time.