1.2.20 · HinglishIntroduction to Programming (Python)

break, continue, pass — when and why

1,602 words7 min readRead in English

1.2.20 · Coding › Introduction to Programming (Python)


YEH HAIN KYA

for i in range(4):
    if i == 2:
        pass          # kuch nahi karta — neeche ka code i==2 ke liye PHIR BHI run hoga
    print(i)          # 0,1,2,3 print karta hai
 
for i in range(4):
    if i == 2:
        continue      # i==2 ke liye print skip karta hai
    print(i)          # 0,1,3 print karta hai

YEH EXIST KYUN KARTE HAIN (first principles)


YEH KAISE BEHAVE KARTE HAIN — 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")   # run hota hai, kyunki break kabhi hua hi nahi

Worked examples


Recall Feynman: ek 12-saal ke bachche ko samjhao

Sochlo tum ghar ghar jaake flyers baant rahe ho.

  • break = tum us insaan ko dhooondh lete ho jise tum dhoondh rahe the, toh ghar chale jaate ho — poori street ke saath done.
  • continue = is ghar mein ek gusse wala kutta hai, toh ise skip karte ho aur seedha agley ghar jaate ho.
  • pass = ek ghar par "no flyers" ka sign hai; tum wahan khade ho, kuch nahi karte, phir apna normal route karte rehte ho. Tumne kuch special skip nahi kiya — bas us ek jagah par karna kuch nahi tha.

Flashcards

break kya karta hai?
Foran nearest enclosing loop se bahar nikalta hai; control loop ke baad wale statement par jaata hai.
continue kya karta hai?
Current iteration ka baaki hissa skip karta hai aur loop ki agli iteration par jump karta hai.
pass kya karta hai?
Kuch nahi — yeh ek no-op placeholder hai jo wahan use hota hai jahan Python ki syntax mein ek statement chahiye.
continue aur pass mein fark?
continue control flow badalta hai (agley iteration par jump karta hai); pass kuch nahi karta aur execution agley line par fall through hoti hai.
Loop ka else clause kab run hota hai?
Sirf tab jab loop normally complete hota hai bina kabhi break execute kiye.
Kya continue loop ke else block ko skip karta hai?
Nahi — else phir bhi run karta hai agar koi break nahi hua.
for i in range(5): (continue if i==1) (break if i==3) else print(i) ka output?
0 aur 2 print karta hai.
Search loop mein break kyun use karo?
Target milne ke baad scanning rokne ke liye, time bachata hai aur result ko overwrite hone se bachata hai.
Nested loops mein kaun sa keyword sirf innermost loop ko affect karta hai?
break aur continue dono sirf nearest enclosing loop ko affect karte hain.
pass ka ek common use?
Kisi function/class/branch ko stub out karna jo baad mein implement karenge, code ko syntactically valid rakhte hue.

Connections

  • For Loopsbreak/continue iteration mechanics par operate karte hain.
  • While Loops — same keywords, continue ke baad condition dubara check hoti hai.
  • for...else and while...elsebreak hi else ko suppress karta hai.
  • Conditionals (if-elif-else) — yeh keywords usually ek if ke andar rehte hain.
  • 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