Control flow · Lesson 10 of 25
Loop control and nesting
Stop a search, skip an item and nest loops.
8 minutes2 games
break exits the nearest loop; continue skips the rest of the current iteration. A nested loop runs for each outer iteration, so track which variable changes and when.
Python
for n in range(1, 8):
if n == 3:
continue
if n == 5:
break
print(n)
for row in range(2):
for col in range(2):
print(row, col)Start iteration
continue: next iteration
break: leave loop
What's the output?
for n in range(4):
if n == 2:
break
print(n)What will this code print?
Exercise
Print the first number above 10 and stop searching.
Expected output: 12
Python
numbers = [4, 12, 20]
Learn more from
Played 0 of 2 games
Sign in to save your progress and earn XP.