whileEasy Examples

Starts a loop that repeats as long as its condition is true

Basic while loop

Repeating code while a condition is true.

python
count = 0
while count < 5:
    print(count)
    count += 1

# Sum until threshold
total = 0
n = 1
while total < 100:
    total += n
    n += 1
print(f"Sum reached {total} after adding {n-1} numbers")
Expected Output
0
1
2
3
4
Sum reached 105 after adding 14 numbers

while repeats its body as long as the condition is True. Make sure the condition eventually becomes False or you get an infinite loop.

Want to try these examples interactively?

Open Easy Playground