⚡0 XP🔥0

break, continue and pass

Lesson 12 of 33 ⏱ 4 min read ✎ 3 exercises ❓ 5-question quiz ▶ Runs Python

Control loops precisely - exit early with break, skip an iteration with continue, and use pass as a placeholder.

Controlling loops#

Normally a loop runs all its iterations. Sometimes you need more control:

Statement What it does
break Exit the loop immediately
continue Skip the rest of this iteration, go to the next one
pass Do nothing (placeholder)

break - exit the loop early#

Use break when you've found what you're looking for, or when there's no reason to continue.

▶ Try it YourselfPython
Ctrl + Enter to run

break with while True#

The most common use: an endless loop that exits when the user wants.

▶ Try it YourselfPython
Ctrl + Enter to run

continue - skip one iteration#

continue jumps straight back to the top of the loop for the next item. The rest of the body is skipped only for this round.

▶ Try it YourselfPython
Ctrl + Enter to run

Filtering bad data with continue#

▶ Try it YourselfPython
Ctrl + Enter to run
हिंदी में समझें

break और continue में फर्क:

  • break = "बस, अब loop से बाहर निकलो।" बाकी बचे सारे iterations छोड़ दिए जाते हैं।
  • continue = "यह वाला round छोड़ दो, अगले round पर जाओ।" Loop चलता रहता है।

उदाहरण: कक्षा में attendance लेते समय -

  • अगर principal आ जाएं और class खत्म करनी पड़े → break
  • अगर कोई student absent है तो उसका नाम छोड़कर अगले पर जाएं → continue

break and continue in nested loops#

They only affect the innermost loop:

▶ Try it YourselfPython
Ctrl + Enter to run

To exit both loops, use a flag variable:

▶ Try it YourselfPython
Ctrl + Enter to run

Loop else with break#

As you saw earlier, a loop's else runs only if no break happened - perfect for search problems:

▶ Try it YourselfPython
Ctrl + Enter to run

pass - the placeholder#

Python doesn't allow empty blocks. pass fills the gap while you plan your code:

for student in students:
    pass          # TODO: send report card

def calculate_tax(amount):
    pass          # will write later

class Invoice:
    pass
Did you know?

pass vs continue: in a loop, pass does nothing and the code after it in the same iteration still runs. continue skips the rest of the iteration.

for i in range(3):
    if i == 1:
        pass
    print(i)      # prints 0 1 2

Summary#

  • break exits the nearest loop immediately.
  • continue skips to the next iteration of the nearest loop.
  • pass does nothing - it is a placeholder for empty blocks.
  • A loop's else runs only when the loop wasn't broken - ideal for searches.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

First multiple of 7

Read numbers from the list given in the code. Print the first number that is divisible by 7, then stop the loop.

Expected output:
49
2

Skip negatives

Print the sum of only the positive numbers in the list, using continue to skip negative numbers.

Expected output:
50
3

Prime check

Read a number n (n ≥ 2) and print Prime or Not prime. Use a for loop with break and the loop's else block.

Test input:
91
Expected output:
Not prime

Quick Quiz

Q1.

What is printed?

for i in range(1, 6):
    if i == 3:
        break
    print(i)
Q2.

What is printed?

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

What does pass do?

Q4.

In nested loops, break inside the inner loop exits...

Q5.

Does the else of a for loop run if the loop ended with break?