# For and While Loops

> How a loop runs the same lines again and again. For loops over a list, while loops on a test, break to stop, and the loop that never ends.

**Course:** [Programming Basics](https://learn.benibix.com/learn/development-concepts)  
**Time:** 13 min

**After this lesson you can:**

- Write a for loop over a list
- Write a while loop that stops
- Spot and fix an infinite loop

## How a program repeats the same lines

A loop runs the same lines again and again. It saves you writing a line 100 times when the job is to do one thing to 100 items. Python has two kinds. A for loop runs once for each item in a collection. A while loop keeps going as long as a test is True.

A for loop reads for name in names:, then a block. Python takes the first item, puts it in name and runs the block. Then it takes the next item, until the list runs out. It stops by itself. To count, use range: for i in range(5): runs with i set to 0, 1, 2, 3 and 4.

range(5) stops before 5. That matches list indexes, which also start at 0. So for i in range(len(names)): visits every spot in the list exactly once. But you rarely need the number. for name in names: is shorter and cannot go past the end.

A while loop checks its test before each pass. while lives > 0: runs until lives drops to 0. Use it when you cannot know the number of passes in advance, such as asking for a password until it is right. Something inside the block must change, or the test stays True for ever.

That is an infinite loop: one that never ends. The program seems to freeze, or it prints the same line without stopping. The usual cause is forgetting to change the value the test checks. In a terminal, Ctrl+C stops a Python program that is stuck.

break stops a loop at once, even mid-list. It is handy when you find what you were looking for. continue is its partner: it skips the rest of this pass and goes straight to the next one. Both work in for and while loops alike.

A common mistake is changing a list while a for loop walks over it. Removing items mid-loop makes Python skip some. Loop over a copy instead, or build a new list with the items you want to keep.

```python
for name in ["Ana", "Ben", "Cy"]:
    print("Hi", name)

lives = 3
while lives > 0:
    print("Lives left:", lives)
    lives = lives - 1     # without this line, it never ends

for n in range(1, 100):
    if n % 7 == 0:
        print("First multiple of 7:", n)
        break
```
A for loop walks a list and stops by itself. A while loop stops only when its test turns False.

![Two flow loops side by side. The for loop takes the next item from a list of three, runs the block, and exits when no items are left. The while loop checks the test lives > 0, runs the block when True, loops back, and exits when the test is False. A dashed path marks break leaving early.](https://learn.benibix.com/diagrams/prog-loops.svg)

### Example: Emma's quiz asks one question for ever

Emma's quiz keeps asking the same question. Her loop is while tries < 3:, but nothing in the block adds 1 to tries.

tries stays at 0, so the test is always True. She adds tries = tries + 1 at the end of the block. Now the quiz stops after 3 tries.

**Words in this lesson:** [Loop](https://learn.benibix.com/term/development/loop), [For Loop](https://learn.benibix.com/term/development/for-loop), [While Loop](https://learn.benibix.com/term/development/while-loop), [Infinite Loop](https://learn.benibix.com/term/development/infinite-loop), [Break Statement](https://learn.benibix.com/term/development/break-statement)

**Question:** Which numbers does for i in range(4): give i?

**Answer:** 0, 1, 2 and 3. range starts at 0 and stops before the number you give it. So range(4) gives 0, 1, 2 and 3, which is 4 passes.

---

Source: https://learn.benibix.com/learn/development-concepts/for-and-while-loops
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
