PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Loop Control: break and continue Statements in Python (लूप नियंत्रण: break (लूप समाप्त) और continue (अगला चक्र)) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: The break and continue statements alter normal loop execution flow inside Python for and while loops. While break immediately exits the innermost enclosing loop, continue skips the remainder of the current iteration and jumps straight to the next cycle.
अध्याय सार: break और continue स्टेटमेंट पायथन के for और while लूप्स में सामान्य निष्पादन प्रवाह को नियंत्रित करते हैं। जहाँ break सबसे भीतरी लूप को तुरंत समाप्त कर देता है, वहीं continue वर्तमान चक्र के शेष कोड को छोड़कर सीधे अगले चक्र पर पहुँच जाता है।
Loop Control: break and continue Statements in Python: Detailed Notes & Architecture (लूप नियंत्रण: break (लूप समाप्त) और continue (अगला चक्र))
1. Mechanism of the break Statement (break स्टेटमेंट की कार्यप्रणाली)
[EN] When Python encounters break inside a loop, it terminates that loop immediately without running any remaining statements in the current body or any future iterations. Control jumps directly to the first statement following the loop block, bypassing the loop's optional else clause as well. Commonly, break stops a linear search once a target element is matched or exits an event loop like while True upon receiving a sentinel input. In nested loops, break exits only the innermost loop containing it, leaving outer loops running normally.
[HI] जब पायथन को लूप के भीतर break मिलता है, तो वह वर्तमान चक्र के शेष स्टेटमेंट्स और आगामी सभी चक्रों को रद्द करके लूप को तुरंत समाप्त कर देता है। नियंत्रण सीधे लूप के बाद वाले पहले स्टेटमेंट पर चला जाता है, और यदि लूप के साथ else क्लॉज जुड़ा हो तो वह भी नहीं चलता। इसका सामान्य उपयोग लक्ष्य मिल जाने पर सर्च रोकने या while True अनंत लूप से बाहर निकलने के लिए किया जाता है। नेस्टेड लूप्स में break केवल उसी सबसे भीतरी लूप से बाहर निकलता है जिसमें वह लिखा हो, बाहरी लूप चलते रहते हैं।
2. Mechanism of the continue Statement and While-Loop Pitfall (continue स्टेटमेंट और while-लूप में सावधानी)
[EN] Executing continue abandons the rest of the statements in the current loop body and starts the next iteration immediately. In a for loop, continue fetches the next element from the iterator; in a while loop, control jumps back to re-evaluate the boolean condition. A classic programming bug occurs in while loops when the loop counter update appears after continue, causing the condition variable to stagnate and triggering an infinite loop. Unlike break, executing continue does not prevent a loop's else clause from executing once all iterations finish naturally.
[HI] continue चलने पर वर्तमान चक्र के नीचे लिखे सभी स्टेटमेंट्स छूट जाते हैं और लूप तुरंत अगले चक्र के लिए तैयार हो जाता है। for लूप में यह इटरेटर से अगला तत्व लेता है, जबकि while लूप में नियंत्रण सीधे शर्त (condition) को दोबारा जाँचने जाता है। while लूप में एक आम गलती तब होती है जब काउंटर बढ़ाने वाला कोड continue के नीचे लिखा हो; इससे काउंटर कभी नहीं बढ़ता और अनंत लूप बन जाता है। break के विपरीत, continue का उपयोग करने पर भी लूप के सामान्य रूप से समाप्त होने पर उसका else क्लॉज चलता है।
3. Interaction with try-finally and Python 3.8+ Rules (try-finally के साथ व्यवहार और पायथन 3.8+ नियम)
[EN] If break or continue is executed inside a try block that has a matching finally clause, Python guarantees that the finally suite executes before leaving or restarting the loop. Historically, prior to Python 3.8, using continue inside a finally block was forbidden and raised SyntaxError due to bytecode stack constraints. Starting with Python 3.8, continue inside a finally clause became fully valid syntax, matching the behavior already allowed for break and return. Using either break or continue outside any enclosing loop—even inside a function defined within a loop—always causes a compile-time SyntaxError.
[HI] यदि break या continue किसी ऐसे try ब्लॉक के अंदर चलता है जिसके साथ finally क्लॉज हो, तो पायथन लूप छोड़ने या अगले चक्र में जाने से पहले finally ब्लॉक को अवश्य निष्पादित करता है। पायथन 3.8 से पहले बाइटकोड सीमाओं के कारण finally ब्लॉक के भीतर continue लिखने पर SyntaxError आता था। पायथन 3.8 से finally के अंदर continue भी पूरी तरह मान्य हो गया है, ठीक वैसे ही जैसे break और return पहले से मान्य थे। किसी भी लूप से बाहर—चाहे लूप के अंदर परिभाषित फंक्शन में ही क्यों न हो—break या continue लिखने पर कंपाइल-टाइम SyntaxError आता है।
4. Exiting Nested Loops Without Labeled Break (नेस्टेड लूप्स से बाहर निकलने की रणनीतियाँ)
[EN] Unlike C, Java, or Go, Python intentionally omits labeled break or goto statements to keep control flow predictable and readable. To break out of multiple nested loops at once, developers wrap the loops inside a helper function and exit immediately using return. Alternatively, you can track a boolean flag checked by the outer loop, raise a custom exception caught outside the outer loop, or flatten iterations using itertools.product(). Checking inner loop termination via a for-else block combined with break in the outer loop is another idiomatic pattern.
[HI] C, Java या Go के विपरीत, पायथन में कोड को सरल और पठनीय रखने के लिए लेबल्ड break (labeled break) या goto की सुविधा नहीं है। एक साथ कई नेस्टेड लूप्स से बाहर निकलने का सबसे स्वच्छ तरीका उन्हें एक हेल्पिंग फंक्शन में रखकर return का उपयोग करना है। अन्य विकल्पों में बाहरी लूप में बूलियन फ्लैग जाँचना, कस्टम एक्सेप्शन उठाकर बाहर पकड़ना, या itertools.product() से लूप्स को समतल करना शामिल है। भीतरी लूप में else के साथ continue और उसके बाद बाहरी लूप में break लिखना भी एक प्रचलित पायथोनिक तरीका है।
Syntax, Formula & Technical Example (Loop Control: break and continue Statements in Python)
results = []
for n in range(1, 8):
if n % 2 == 0:
continue # skip even numbers
if n == 7:
break # stop loop when n reaches 7
results.append(n)
print(results) # [1, 3, 5]
Loop Control: break and continue Statements in Python Quick Reference & Comparison Table
| Control Statement | Effect on Loop Execution | Impact on Loop else & Scope |
|---|---|---|
| break | Terminates the innermost loop immediately | Skips the loop's else clause completely |
| continue | Skips rest of current body; starts next cycle | Allows the loop's else clause to run upon finish |
| return (in loop) | Exits both the loop and the enclosing function | Bypasses loop else; still triggers finally block |
| pass (in loop) | Performs null operation; continues same iteration | Does not skip remaining statements in the iteration |
Loop Control: break and continue Statements in Python Top 20 Bilingual MCQs with Answers
Q1. What does the following code print? for i in range(5): if i == 3: break print(i, end=' ')
(निम्न कोड का आउटपुट क्या होगा? for i in range(5): if i == 3: break print(i, end=' '))
(A) 0 1 2 3 4
(B) 0 1 2
(C) 0 1 2 4
(D) 0 1 2 3
Correct Answer: (B)
[EN] When i becomes 3, break exits the loop before print(i) runs, so only 0 1 2 are printed.
[HI] जब i का मान 3 होता है तो print(i) चलने से पहले ही break लूप समाप्त कर देता है, अतः 0 1 2 प्रिंट होता है।
Q2. What does the following code print? for i in range(5): if i == 2: continue print(i, end=' ')
(निम्न कोड का आउटपुट क्या होगा? for i in range(5): if i == 2: continue print(i, end=' '))
(A) 0 1 2 3 4
(B) 2
(C) 0 1
(D) 0 1 3 4
Correct Answer: (D)
[EN] continue skips print(i) only when i == 2 and moves on to iterations 3 and 4.
[HI] continue केवल i == 2 होने पर print(i) को छोड़ता है और अगले चक्रों 3 व 4 के लिए लूप जारी रखता है।
Q3. In nested loops, which loop does a break statement inside the inner loop terminate?
(नेस्टेड लूप्स में, भीतरी लूप के भीतर लिखा break स्टेटमेंट किस लूप को समाप्त करता है?)
(A) Only the outermost loop
(B) Only the innermost enclosing loop
(C) All enclosing loops at once
(D) The enclosing function itself
Correct Answer: (B)
[EN] Python's break statement affects only the immediate innermost loop that contains it.
[HI] पायथन का break स्टेटमेंट केवल उसी सबसे भीतरी लूप को समाप्त करता है जिसमें वह स्थित होता है।
Q4. What happens if you place a break statement outside any loop in Python?
(पायथन में किसी लूप के बाहर break स्टेटमेंट लिखने पर क्या होता है?)
(A) RuntimeError during execution
(B) It exits the Python program
(C) SyntaxError at compile time
(D) It acts like pass
Correct Answer: (C)
[EN] Using break or continue outside a loop violates grammar rules and raises SyntaxError: 'break' outside loop.
[HI] लूप के बाहर break या continue लिखना व्याकरण नियम के विरुद्ध है और कंपाइल समय पर SyntaxError देता है।
Q5. What is the output of this while loop? i = 0 while i < 4: i += 1 if i == 2: continue print(i, end='')
(इस while लूप का आउटपुट क्या होगा? i = 0 while i < 4: i += 1 if i == 2: continue print(i, end=''))
(A) 134
(B) 1234
(C) Infinite loop
(D) 0134
Correct Answer: (A)
[EN] i increments before continue, so i reaches 1, skips printing 2, then prints 3 and 4.
[HI] continue से पहले i बढ़ जाता है, इसलिए 1 प्रिंट होता है, 2 छूटता है, फिर 3 और 4 प्रिंट होते हैं।
Q6. What bug happens if `i += 1` is written AFTER `continue` inside a while loop where `if i == 2: continue` runs?
(यदि while लूप में `if i == 2: continue` के बाद `i += 1` लिखा जाए तो क्या समस्या होगी?)
(A) i skips straight to 4
(B) SyntaxError is raised
(C) An infinite loop occurs when i equals 2
(D) The loop terminates via break
Correct Answer: (C)
[EN] When i becomes 2, continue jumps back to the while check before i += 1 runs, keeping i == 2 forever.
[HI] i के 2 होने पर continue के कारण i += 1 कभी नहीं चलता और i हमेशा 2 रहकर अनंत लूप बना देता है।
Q7. What happens when continue is executed inside a try block that has a finally clause?
(जब finally क्लॉज वाले try ब्लॉक के अंदर continue चलता है, तो क्या होता है?)
(A) The finally block is skipped completely
(B) break is triggered instead
(C) Python raises RuntimeError
(D) The finally block executes before the next loop iteration starts
Correct Answer: (D)
[EN] Python guarantees finally always executes before control transfers to the next iteration of the loop.
[HI] पायथन में अगले चक्र पर नियंत्रण जाने से पहले finally ब्लॉक का निष्पादन निश्चित रूप से होता है।
Q8. Does Python support labeled break statements such as `break outer;`?
(क्या पायथन में `break outer;` जैसे लेबल्ड break स्टेटमेंट का समर्थन है?)
(A) Yes, since Python 3.10
(B) Yes, using @label decorators
(C) Only for while loops, not for loops
(D) No; Python does not support labeled break or continue
Correct Answer: (D)
[EN] Python has no labeled break syntax; helper functions with return or flags are used to exit nested loops.
[HI] पायथन में लेबल्ड break सिंटैक्स नहीं है; नेस्टेड लूप्स से बाहर निकलने के लिए फंक्शन में return या फ्लैग प्रयोग होते हैं।
Q9. In which Python version was using `continue` inside a `finally` block first allowed?
(किस पायथन संस्करण से `finally` ब्लॉक के भीतर `continue` का उपयोग वैध किया गया?)
(A) Python 3.0
(B) Python 2.7
(C) Python 3.8
(D) Python 3.12
Correct Answer: (C)
[EN] Before Python 3.8, continue inside a finally block raised SyntaxError; Python 3.8 lifted that restriction.
[HI] पायथन 3.8 से पहले finally ब्लॉक में continue लिखने पर SyntaxError आता था; 3.8 में यह पाबंदी हटा दी गई।
Q10. What will this code print? for x in [1, 2, 3]: if x == 2: continue else: print('Done')
(निम्न कोड क्या प्रिंट करेगा? for x in [1, 2, 3]: if x == 2: continue else: print('Done'))
(A) Done
(B) Nothing is printed
(C) SyntaxError
(D) 2
Correct Answer: (A)
[EN] Only break prevents the loop's else block from running; continue lets the loop finish normally, so Done prints.
[HI] केवल break ही लूप के else ब्लॉक को रोकता है; continue से लूप सामान्य रूप से पूरा होता है और Done प्रिंट होता है।
Q11. What is printed by the following snippet? for a in range(2): for b in range(3): if b == 1: break print(a, b)
(निम्न कोड स्निपेट क्या प्रिंट करेगा? for a in range(2): for b in range(3): if b == 1: break print(a, b))
(A) 0 0 followed by 1 0
(B) 0 0, 0 1, 1 0, 1 1
(C) 0 0, 0 2, 1 0, 1 2
(D) 0 0 only
Correct Answer: (A)
[EN] break exits only the inner loop when b == 1, so the outer loop still runs for a = 0 and a = 1.
[HI] b == 1 होने पर break केवल भीतरी लूप तोड़ता है, इसलिए बाहरी लूप a = 0 और a = 1 दोनों के लिए चलता है।
Q12. Which statement best differentiates break from continue?
(break और continue के बीच सबसे सटीक अंतर कौन-सा कथन बताता है?)
(A) break exits the loop completely, whereas continue skips only the current iteration
(B) break skips one iteration, whereas continue terminates the program
(C) break works only in while loops, whereas continue works only in for loops
(D) break executes the else block, whereas continue skips the else block
Correct Answer: (A)
[EN] break aborts the entire enclosing loop; continue jumps to the next iteration check.
[HI] break पूरे लूप को समाप्त करता है, जबकि continue केवल वर्तमान चक्र के शेष हिस्से को छोड़कर अगले चक्र पर जाता है।
Q13. What happens if break is executed inside a nested function defined inside a for loop?
(यदि किसी for लूप के अंदर परिभाषित नेस्टेड फंक्शन के भीतर break लिखा जाए तो क्या होगा?)
(A) It skips one iteration of the outer loop
(B) SyntaxError because break does not bind across function boundaries
(C) It breaks the outer for loop
(D) It returns None from the function
Correct Answer: (B)
[EN] Loops are lexically scoped per function; a break inside def f(): must belong to a loop inside f().
[HI] फंक्शन की सीमा के पार लूप लागू नहीं होता; फंक्शन के अंदर break लिखने पर SyntaxError आएगा जब तक वह स्वयं किसी भीतरी लूप में न हो।
Q14. How many times does 'Hi' print? for x in range(10): if x > 2: break if x % 2 == 0: continue print('Hi')
(निम्न कोड में 'Hi' कितनी बार प्रिंट होगा? for x in range(10): if x > 2: break if x % 2 == 0: continue print('Hi'))
(A) 2
(B) 5
(C) 3
(D) 1
Correct Answer: (D)
[EN] x=0 triggers continue, x=1 prints 'Hi', x=2 triggers continue, and x=3 triggers break. Total prints: 1.
[HI] x=0 पर continue, x=1 पर 'Hi' प्रिंट, x=2 पर continue और x=3 पर break चलता है। कुल 1 बार प्रिंट होता है।
Q15. Which approach is recommended in Python to exit three nested loops cleanly when a match is found?
(किसी लक्ष्य के मिलने पर तीन नेस्टेड लूप्स से स्वच्छ रूप से बाहर निकलने का पायथन में अनुशंसित तरीका कौन-सा है?)
(A) Use continue outer
(B) Use break 3
(C) Wrap the nested loops in a function and use return
(D) Use goto end_loops
Correct Answer: (C)
[EN] Placing nested loops inside a helper function and returning upon finding the match avoids flags and extra checks.
[HI] नेस्टेड लूप्स को एक फंक्शन में रखकर return करना बिना किसी अतिरिक्त फ्लैग के तुरंत बाहर निकलने का सबसे स्वच्छ तरीका है।
Q16. Which statement about continue in Python is NOT true?
(पायथन में continue स्टेटमेंट के बारे में निम्नलिखित में से कौन-सा कथन असत्य (NOT true) है?)
(A) continue prevents the loop's else clause from executing after the loop finishes
(B) continue raises SyntaxError when placed outside a loop
(C) continue skips remaining code in the current loop body
(D) continue can be used inside both for and while loops
Correct Answer: (A)
[EN] A loop's else block is skipped only by break (or return/exception), never by continue.
[HI] लूप का else क्लॉज केवल break से रुकता है, continue का उपयोग करने पर भी लूप समाप्त होने पर else चलता है।
Q17. Within Loop Control: break and continue Statements in Python, which control statement directly maps to: "Terminates the innermost loop immediately"?
(Loop Control: break and continue Statements in Python में कौन सा Control Statement सीधे "Terminates the innermost loop immediately" से संबंधित है?)
(A) break
(B) continue
(C) pass (in loop)
(D) return (in loop)
Correct Answer: (A)
[EN] In Loop Control: break and continue Statements in Python, 'break' handles 'Terminates the innermost loop immediately' (Skips the loop's else clause completely).
[HI] Loop Control: break and continue Statements in Python में 'break' का कार्य/विवरण 'Terminates the innermost loop immediately' (Skips the loop's else clause completely) है।
Q18. Which option accurately describes 'continue' in Loop Control: break and continue Statements in Python?
(Loop Control: break and continue Statements in Python के संदर्भ में 'continue' की सही विशेषता कौन सी है?)
(A) Performs null operation; continues same iteration
(B) Skips rest of current body; starts next cycle
(C) Terminates the innermost loop immediately
(D) Exits both the loop and the enclosing function
Correct Answer: (B)
[EN] 'continue' provides 'Skips rest of current body; starts next cycle' — key point: Allows the loop's else clause to run upon finish.
[HI] 'continue' का मुख्य विवरण 'Skips rest of current body; starts next cycle' है (Allows the loop's else clause to run upon finish)।
Q19. In Loop Control: break and continue Statements in Python, identify the correct parameter for the specification: "Exits both the loop and the enclosing function".
(Loop Control: break and continue Statements in Python में "Exits both the loop and the enclosing function" विवरण किस विकल्प के लिए सही है?)
(A) continue
(B) pass (in loop)
(C) return (in loop)
(D) break
Correct Answer: (C)
[EN] 'return (in loop)' corresponds to 'Exits both the loop and the enclosing function' (Bypasses loop else; still triggers finally block).
[HI] 'return (in loop)' का संबंध 'Exits both the loop and the enclosing function' (Bypasses loop else; still triggers finally block) से है।
Q20. Considering Loop Control: break and continue Statements in Python, what key behavior defines 'pass (in loop)'?
(Loop Control: break and continue Statements in Python में 'pass (in loop)' की मुख्य भूमिका या मान क्या है?)
(A) Performs null operation; continues same iteration
(B) Terminates the innermost loop immediately
(C) Skips rest of current body; starts next cycle
(D) Exits both the loop and the enclosing function
Correct Answer: (A)
[EN] 'pass (in loop)' represents 'Performs null operation; continues same iteration' (Does not skip remaining statements in the iteration).
[HI] 'pass (in loop)' का अर्थ/मान 'Performs null operation; continues same iteration' है (Does not skip remaining statements in the iteration)।
Loop Control: break and continue Statements in Python — Exam FAQs
Q1: Why did Python omit labeled break statements?
पायथन में लेबल्ड break स्टेटमेंट क्यों नहीं दिए गए?
[EN] PEP 3136 rejected labeled break/continue because deeply nested loops that need multi-level jumps are usually clearer when refactored into small helper functions with return.
[HI] PEP 3136 ने लेबल्ड break/continue को अस्वीकार किया क्योंकि गहरे नेस्टेड लूप्स से बाहर निकलने के लिए छोटे हेल्पिंग फंक्शन में return का उपयोग अधिक स्पष्ट होता है।
Q2: What is the difference between continue and pass inside a loop?
लूप के अंदर continue और pass में क्या अंतर है?
[EN] continue immediately jumps to the next loop iteration and skips any statements below it, whereas pass does nothing and lets execution continue down the rest of the current iteration.
[HI] continue तुरंत नीचे के कोड को छोड़कर अगले चक्र पर चला जाता है, जबकि pass कुछ नहीं करता और वर्तमान चक्र का शेष कोड चलता रहता है।
Q3: Does break exit an if-statement or a loop?
क्या break केवल if-ब्लॉक से बाहर निकलता है या पूरे लूप से?
[EN] break always exits the nearest enclosing loop (for or while), never an if statement by itself.
[HI] break हमेशा निकटतम लूप (for या while) से बाहर निकलता है, अकेले if स्टेटमेंट से नहीं।
Post a Comment
Post a Comment