A loop in programming is a structure that repeats a block of code, either a fixed number of times or until a condition stops being true. It is one of the most fundamental ideas in coding because so much of programming is doing the same thing to many items: sending an email to every customer, adding up every number in a list, or retrying a request until it succeeds. Instead of copying the same lines dozens of times, you write them once inside a loop and let the computer repeat them for you.
Why loops exist
Imagine you need to print every name in a list of a hundred people. Without a loop you would write a hundred nearly identical lines. With a loop you write the instruction once and tell it to run for each name. Loops turn repetitive work into a few lines that scale to any size, whether the list has three items or three million.
The block of code that repeats is called the loop body. Each single run through the body is called an iteration. Loops are one of the first building blocks you meet when you start coding.
The two main kinds of loop
Most languages give you two core loop types, and the choice comes down to whether you know in advance how many times to repeat.
| Loop type |
Use when |
Repeats |
| For loop |
You know how many times, or are walking a collection |
A set number of times |
| While loop |
You repeat until a condition changes |
As long as a condition is true |
// a for loop: run a known number of times
for i in range(3):
print("hello") // prints hello three times
// a while loop: run until a condition flips
count = 0
while count < 3:
print(count)
count = count + 1 // without this, the loop never ends
A for loop is the natural fit for "do this for every item." A while loop fits "keep going until something happens," like reading input until the user types quit.
Steering a loop with break and continue
Sometimes you want to leave a loop early or skip an iteration. Two keywords handle this:
- break stops the loop entirely and moves on to the code after it.
- continue skips the rest of the current iteration and jumps to the next one.
Used carefully these make loops cleaner. Used carelessly they make logic hard to follow, so reach for them when they genuinely simplify the code.
How to write loops well
- Pick a for loop when the count or collection is known, and a while loop when you repeat until a condition changes.
- Make sure a while loop can actually end; something inside it must eventually make the condition false.
- Keep the loop body small; if it grows large, move the work into a function and call it from the loop.
- Use break and continue sparingly and only when they make intent clearer.
- Watch performance on huge collections; an inefficient loop inside another loop can get slow fast.
What to skip
- Skip infinite loops with no exit. If the condition never becomes false, the program hangs. Always have a clear stopping point.
- Skip changing a collection while looping over it. It causes confusing bugs in many languages.
- Skip deeply nested loops when a clearer approach exists; they get hard to read and slow quickly.
- Skip manual counting when the language offers a clean way to loop directly over items.
FAQ
What is the difference between a for loop and a while loop?
A for loop is best when you know how many times to repeat or are walking through a collection. A while loop repeats as long as a condition stays true, which suits cases where you do not know the count in advance.
What is an infinite loop?
A loop whose stopping condition never becomes false, so it runs forever and freezes the program. It is usually a bug, caused by forgetting to update the value the condition checks.
What do break and continue do?
Break exits the loop immediately and continues after it. Continue skips the rest of the current iteration and moves to the next one. Both let you control the flow without rewriting the loop.
Are loops slow?
A loop itself is fast, but the total time depends on how many iterations run and how heavy the body is. Loops nested inside loops over large data can become slow, so watch that case.
Where to go next
Learn how to write a function, understand what a boolean is, and start coding from the basics.