Contact
Evil Genius SocietyHomeLearnForumLeaderboardSocialsMessages
← Back to module

Lesson 7 of 10

Sometimes a loop needs to stop early, or skip just one round. Python gives you three small keywords for that.

break — leave the loop now

break exits the loop immediately. Nothing after it in the body runs, and the loop does not continue.

for i in range(1, 11):
    if i == 4:
        break
    print(i)

print("Stopped.")

That prints 1, 2, 3 and then stops — break fires before print(i) on the fourth pass.

A common use is searching: once you have found what you want, there is no reason to keep looking.

for letter in "python":
    if letter == "h":
        print("Found it!")
        break

continue — skip to the next round

continue skips the rest of the body and jumps straight to the next iteration. The loop keeps going.

for i in range(1, 6):
    if i == 3:
        continue
    print(i)

That prints 1, 2, 4, 5 — three is skipped, but the loop finishes normally.

pass — do nothing

pass is a placeholder that does nothing at all. Python requires an indented body after a colon, so pass lets you write the structure before you write the logic.

for i in range(3):
    pass

print("Loop ran, body was empty.")

You will mostly use pass while sketching out code you intend to fill in later.

break in a while loop

break works the same way in while loops, and is often how you exit a deliberately infinite one:

count = 0

while True:
    count += 1
    if count == 3:
        break

print(count)

while True never ends on its own — the break is the only exit. Use this pattern carefully, and always make sure there is a reachable break.

Try it yourself

Loop from 1 to 10, skip the number 3 with continue, and stop entirely at 6 with break.

Checkpoint

  • break exits the loop immediately.
  • continue skips the rest of this iteration and starts the next.
  • pass does nothing — it is a structural placeholder.
  • while True plus break is a valid pattern when the exit condition is easier to check mid-loop.

Questions about this lesson

Log in to ask a question.