break, continue and else
break stops the loop. continue skips to the next round. A loop can have an else block that runs only if the loop finished without hitting break, which is handy for searches.
Example
for n in range(1, 8):
if n == 3:
continue
if n == 6:
break
print(n)
for n in [2, 4, 6]:
if n % 2:
print("found odd")
break
else:
print("no odd numbers")Result
1 2 4 5 no odd numbers
Exercise
Skip to the next round.
for n in range(5):
if n == 2:
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.