# While Loop

> A loop that keeps going while a test is true, and stops when it turns false.

**Field:** Software Development  
**Category:** Programming Concepts  
**Also known as:** while

## Think of it like

Think of it like stirring soup while it is still lumpy. You stop the moment it is smooth.

## Definition

A while loop keeps running as long as a test stays true. It checks the test before every pass. When the test turns false, the loop stops. You use it when you do not know ahead of time how many passes you need. Something inside the loop must change the answer, or it never ends.

## Example

A rocket game counts down with a while loop. It prints 3, 2, 1, then the test n > 0 turns false and it prints Go!

## Example code

```python
n = 3
while n > 0:
    print(n)
    n = n - 1
print("Go!")
```

It prints 3, 2 and 1, then Go! once n reaches 0.

## Why it matters

Some jobs have no fixed count, like wait until the file is ready. A while loop fits those.

## Common mistake

Forgetting to change anything inside the loop. The test stays true and the program hangs.

## Three principles

- **Test before each pass.** It checks, then runs, then checks again.
- **Unknown count.** Use it when you cannot count passes ahead.
- **Change the test.** Something inside must move it toward false.

## Related terms

- [Loop](https://learn.benibix.com/term/development/loop)
- [For Loop](https://learn.benibix.com/term/development/for-loop)
- [Infinite Loop](https://learn.benibix.com/term/development/infinite-loop)
- [Boolean](https://learn.benibix.com/term/development/boolean)

## Told apart from

- [For Loop vs While Loop](https://learn.benibix.com/compare/for-loop-vs-while-loop): A for loop goes over a list and stops when the list runs out. A while loop goes on until its test turns false.

## Taught in

- [For and While Loops](https://learn.benibix.com/learn/development-concepts/for-and-while-loops): Programming Basics, 11/18

---

Source: https://learn.benibix.com/term/development/while-loop
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.
