Imagine having to print “Hello” 100 times. You could write 100 lines of code – or you could write one loop.
That is exactly what loops are for. They let you repeat a block of code as many times as you need â without writing it over and over again.
Once you understand loops, you will write cleaner, smarter, and faster code from day one.
What Is a Loop in Programming?

What if you had to print “Hello” 100 times? Without a loop, that’s 100 lines of code. With a loop â it’s 2. Let me show you exactly what that means.
Simple Definition
A loop is an instruction that tells your program to repeat a block of code multiple times â automatically. You write the code once, and the loop runs it again and again until you tell it to stop.
A loop repeats a block of code over and over â either a set number of times, or until a condition becomes false. That’s it. One idea, endless power.
Real-Life Analogy â Your Morning Routine
Think about your morning routine. Every single day you do the same steps â in the same order. You don’t write a new plan each morning. You just repeat it automatically.
Without Loops vs With Loops
Say you want to print “Hello” five times. Without a loop, you write the same line five times. Imagine doing that for 100 or 1000 times â it becomes impossible fast.
print(“Hello”)
print(“Hello”)
print(“Hello”)
print(“Hello”)
# 5 lines for 5 prints
# 1000 prints = 1000 lines đŠ
print(“Hello”)
# Just 2 lines
# Change 5 to 1000?
# Still just 2 lines â
The For Loop â When You Know How Many Times
The most common loop â perfect when the count is fixed.
The for loop is the most common loop in coding. Use it when you know exactly how many times you want something to repeat.
For Loop in JavaScript
console.log(i);
}
For Loop in Python
Python’s for loop is even cleaner â no counter setup needed. Just use range() to tell it how many times.
print(i) # 0, 1, 2, 3, 4
let i = 0, condition, and i++ â manual setuprange(n) â cleaner, automatic counterBreaking Down the For Loop
The JavaScript for loop has three parts inside the brackets â each one has a specific job.
console.log(i);
}
The While Loop â When You Don’t Know How Many Times
A for loop is great when you know the count. But what if you don’t? What if you want to keep going until something happens? That’s exactly what the while loop is for.
A while loop checks a condition first. If it’s true â it runs the code. Then checks again. And keeps going until the condition turns false.
While Loop in JavaScript + Python
Same idea in both â keep running while the condition stays true. Count up to 5 and stop.
while (i <= 5) {
console.log(i);
i++; // â must update!
}
// OUTPUT â 1 2 3 4 5
while i <= 5:
print(i)
i += 1 # â must update!
# OUTPUT â 1 2 3 4 5
Watch out â Infinite Loop! If you forget to update i, the condition never turns false and your loop runs forever â crashing your program. Always make sure your loop has a way to stop.
For vs While â When to Use Which
| Situation | Best Loop | Example |
|---|---|---|
| You know the exact count | for loop | Print numbers 1â10 |
| Loop through a list | for loop | Go through every item in an array |
| You don’t know when it stops | while loop | Keep asking until user types “quit” |
| Waiting for a condition | while loop | Keep retrying until connection succeeds |
The Do/While Loop â Run at Least Once
The do/while loop is the least common â but it solves one specific problem perfectly: what if you need the code to run at least once, no matter what?
The only difference between while and do/while is when the condition is checked. While checks first â do/while runs first, then checks.
Do/While in JavaScript
The do block runs first. Then the while condition is checked. If false â it stops. Here is a classic example: asking for a password until the user gets it right.
do {
password = prompt(“Enter password:”);
} while (password !== “secret123”);
console.log(“Access granted!”);
// Always asks at least once â
// Keeps asking until correct â
When to Use Do/While
â Good fit for:
â Not ideal when:
Looping Through Arrays
This is where loops become truly powerful. Going through every item in a list â one by one â is something you’ll do in almost every program you ever write.
Classic For Loop with Arrays
The classic approach uses an index counter i to step through each slot in the array â starting at 0, ending at the last item.
for (let i = 0; i < names.length; i++) {
console.log(names[i]);
}
// OUTPUT â Ali Sara Hamza Zara
for…of â The Cleaner Way â Recommended
The for...of loop is the modern, cleaner way to loop through arrays in JavaScript. No index counter needed â it just grabs each value directly.
for (let name of names) {
console.log(name); // each value directly
}
// OUTPUT â Ali Sara Hamza Zara
Python for Loop with Lists
Python’s for loop is even simpler. It works directly on lists â no index, no length check. Just loop through each item naturally.
for name in names:
print(name)
# OUTPUT â Ali Sara Hamza Zara
i, no length, no brackets. Just for item in list â and Python handles the rest automatically.for...of for clean code. In Python, just use for item in list â it’s that simple.Break and Continue â Controlling Loops
Sometimes you need to stop a loop early. Other times you want to skip one item and move on. That’s exactly what break and continue are for.
What Does Break Do?
break stops the loop completely â right where it is. The moment the program hits break, it exits the loop and moves on.
if (i === 4) break; // stop at 4
console.log(i);
}
// OUTPUT â 1 2 3
What Does Continue Do?
continue skips the current item and jumps straight to the next one. The loop keeps going â it just skips whatever you tell it to skip.
if (i === 3) continue; // skip 3
console.log(i);
}
// OUTPUT â 1 2 4 5
Code Examples in Python
if i == 4:
break
print(i)
# OUTPUT â 1 2 3
if i == 3:
continue
print(i)
# OUTPUT â 1 2 4 5
Exits the entire loop immediately. Nothing after it runs in this loop.
Skips only the current item. Loop continues with the next one.
Common Loop Patterns Every Beginner Should Know
These three patterns show up in real projects constantly. Once you know them, you’ll recognize them everywhere â and writing loops will start to feel natural.
Sum All Numbers in a List
Start with a total of 0. Loop through each number and add it to the total. Common use: calculating a shopping cart total, averaging scores.
total = 0 # start at 0
for num in numbers:
total += num # add each number
print(total)
let total = 0; // start at 0
for (let num of numbers) {
total += num; // add each number
}
console.log(total);
Find an Item in a List
Loop through each item and check if it matches what you’re looking for. When found â print it and stop with break. Common use: searching for a user, product, or name.
for name in names:
if name == “Hamza”:
print(“Found:”, name)
break
for (let name of names) {
if (name === “Hamza”) {
console.log(“Found:”, name);
break;
}
}
Count How Many Times Something Appears
Start a counter at 0. Loop through the list and add 1 every time you find a match. Common use: counting votes, occurrences, or how many items pass a condition.
count = 0 # start counting at 0
for score in scores:
if score >= 70: # passing score?
count += 1
print(“Passed:”, count)
let count = 0; // start counting at 0
for (let score of scores) {
if (score >= 70) { // passing score?
count++;
}
}
console.log(“Passed:”, count);
FAQs About Loops
Three questions beginners always ask about loops â answered simply and clearly.
What is the most common loop in programming?
âŧ
The for loop is by far the most used loop in programming. Most tasks involve repeating something a known number of times â or going through every item in a list â and the for loop handles both perfectly.
What is an infinite loop?
âŧAn infinite loop is a loop that never stops â because its condition never turns false. It keeps running forever, eventually freezing or crashing your program.
while (true) {
console.log(“Help! I can’t stop!”);
}
# Python â forgot to update i
i = 1
while i < 5:
print(i)
# i never updates â runs forever!
Two most common causes: condition is always true, or you forgot to update the counter variable so the condition never changes.
Fix: always make sure your loop has a clear exit. Update i inside while loops. If your program freezes â press Ctrl + C to stop it.
What is the difference between break and continue?
âŧ
Both control how a loop runs â but they do opposite things. break ends the loop entirely. continue only skips the current item and keeps the loop going.
break
# loop ends here
continue
# jumps to next item
