Beginner Fundamentals
While loops
while repeats a block while the condition is true.
i = 1
while i <= 5:
print(i)
i += 1
Without i += 1, the condition never becomes false and the loop runs forever (infinite loop).
break
Exit the loop immediately:
i = 1
while True:
if i > 3:
break
print(i)
i += 1
continue
Skip to the next iteration:
i = 0
while i < 5:
i += 1
if i == 3:
continue # skip 3
print(i)
else on while
Runs when the loop ends normally (no break):
i = 1
while i <= 3:
print(i)
i += 1
else:
print("done")