1.2.20Introduction to Programming (Python)

break, continue, pass — when and why

1,688 words8 min readdifficulty · medium1 backlinks

WHAT they are

for i in range(4):
    if i == 2:
        pass          # does nothing — code below STILL runs for i==2
    print(i)          # prints 0,1,2,3
 
for i in range(4):
    if i == 2:
        continue      # skips print for i==2
    print(i)          # prints 0,1,3

WHY they exist (first principles)


HOW they behave — the mental model

Figure — break, continue, pass — when and why
for x in [1, 3, 5]:
    if x % 2 == 0:
        break
else:
    print("no even number found")   # runs, because break never happened

Worked examples


Recall Feynman: explain to a 12-year-old

Imagine you're handing out flyers door to door.

  • break = you find the person you were looking for, so you go home — done with the whole street.
  • continue = this house has an angry dog, so you skip it and move straight to the next house.
  • pass = a house has a sign "no flyers"; you stand there, do nothing, then keep doing your normal route. You didn't skip anything special — you just had nothing to do at that one spot.

Flashcards

What does break do?
Immediately exits the nearest enclosing loop; control goes to the statement after the loop.
What does continue do?
Skips the rest of the current iteration and jumps to the loop's next iteration.
What does pass do?
Nothing — it's a no-op placeholder used where Python's syntax requires a statement.
Difference between continue and pass?
continue changes control flow (jumps to next iteration); pass does nothing and execution falls through to the next line.
When does a loop's else clause run?
Only when the loop completes normally without ever executing break.
Does continue skip the loop's else block?
No — else still runs if no break occurred.
Output of for i in range(5): (continue if i==1) (break if i==3) else print(i)?
Prints 0 and 2.
Why use break in a search loop?
To stop scanning once the target is found, saving time and avoiding overwriting the result.
Which keyword affects only the innermost loop when nested?
Both break and continue affect only the nearest enclosing loop.
A common use of pass?
Stubbing out a function/class/branch you'll implement later, keeping code syntactically valid.

Connections

  • For Loopsbreak/continue operate on iteration mechanics.
  • While Loops — same keywords, condition re-checked after continue.
  • for...else and while...elsebreak is what suppresses the else.
  • Conditionals (if-elif-else) — these keywords usually live inside an if.
  • Functions and defpass as a stub body.
  • Prime Number Checking — classic while...else + break pattern.

Concept Map

need to exit now

need to skip item

placeholder

terminates

jumps to

skips

skips rest of

jumps to

else still runs

does

execution continues to

all replace

all replace

Loop needs mid-body control

break

continue

Syntax requires a statement

pass

Nearest enclosing loop

First statement after loop

Loop else clause

Current iteration body

Next iteration

No operation

Very next line

Ugly flag variables

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Socho ek loop ek kaam baar-baar kar raha hai. Teen situations aati hain. Pehli: kaam ho gaya, ab loop ko poora band karna hai — uske liye break. Jaise list mein naam dhoondh rahe ho aur mil gaya, to aage scan karne ka koi matlab nahi, seedha break maar do.

Doosri: bas is baar wale item ko skip karna hai, par loop chalta rahe — uske liye continue. Jaise 1 se 10 tak chal rahe ho aur 3 ke multiples ko add nahi karna, to continue likho aur baaki neeche ka code is iteration mein skip ho jayega, agla number aa jayega.

Teesri: kabhi-kabhi Python ko ek statement chahiye hota hai (jaise if: ya def: ke baad block khaali nahi ho sakta), par tumhare paas abhi karne ko kuch nahi. Tab pass lagao — ye kuch nahi karta, sirf jagah bharta hai. Yaad rakho: continue aur pass same nahi hain. continue control flow badalta hai (agle round pe jump), jabki pass kuch nahi karta aur agli line normally chalti rahti hai.

Ek important cheez: loop ke saath else bhi aata hai. Wo else tabhi chalta hai jab loop bina break ke poora ho jaye. Isliye prime number check jaise patterns mein ye bahut clean lagta hai — flag variable ki zaroorat hi nahi padti.

Go deeper — visual, from zero

Test yourself — Introduction to Programming (Python)

Connections