while Loop (Python)

A while loop repeats a block of code as long as a condition stays True. It is used when you don't know in advance how many times you need to loop.

Python

Make sure something inside the loop eventually makes the condition False, otherwise you create an infinite loop. Use break to exit early and continue to skip to the next iteration.

Syntax

while condition:
    # do something

Examples

Python

count = 0
while count < 3:
    print(count)
    count += 1

Output:

0
1
2

Learn it in a lesson

See also

Back to the Dictionary