Codzy

While loops, break and continue

A while loop repeats a block as long as a condition is true. Careful: the condition must eventually become false, otherwise the loop never stops (infinite loop).

Inside a loop, break stops the loop immediately, and continue skips the rest of the current round and jumps straight to the next one.

count = 3
while count > 0:
    print(count)
    count -= 1
print("Go!")

for n in range(10):
    if n == 4:
        break
    print(n)

Start the lesson