PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम The else Clause with Loops (for-else and while-else) (लूप्स के साथ else क्लॉज: केवल स्वाभाविक समाप्ति पर चलना) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Python allows an optional else clause directly on both for and while loops, running only when the loop completes without executing a break statement. Conceptually acting as a 'no-break' block, loop-else eliminates manual boolean flag variables in linear search algorithms.
अध्याय सार: पायथन में for और while दोनों लूप्स के साथ एक वैकल्पिक else क्लॉज जोड़ा जा सकता है, जो केवल तभी चलता है जब लूप बिना break स्टेटमेंट चलाए स्वाभाविक रूप से समाप्त हो। यह व्यावहारिक रूप से 'नो-ब्रेक' (no-break) ब्लॉक की तरह काम करता है और लीनियर सर्च में अलग बूलियन फ्लैग वेरिएबल की जरूरत खत्म करता है।
The else Clause with Loops (for-else and while-else): Detailed Notes & Architecture (लूप्स के साथ else क्लॉज: केवल स्वाभाविक समाप्ति पर चलना)
1. Execution Semantics: Natural Termination vs break (निष्पादन नियम: स्वाभाविक समाप्ति बनाम break)
[EN] In a for-else construct, the else block runs immediately after the loop exhausts every item in the iterable without hitting break. In a while-else construct, the else block executes as soon as the while condition evaluates to False naturally. Whenever control exits the loop early via break, return, or an unhandled exception, the else suite is completely skipped. Because of this behavior, many Python experts advise reading the loop's else keyword mentally as 'no break' rather than 'otherwise'.
[HI] for-else संरचना में else ब्लॉक तभी चलता है जब लूप बिना break चलाए इटरेबल के सभी तत्वों को समाप्त कर दे। while-else में else ब्लॉक तब चलता है जब while की शर्त स्वाभाविक रूप से False हो जाए। यदि लूप बीच में break, return या किसी अनहैंडल्ड एक्सेप्शन से बाहर निकलता है, तो else ब्लॉक बिल्कुल नहीं चलता। इसी व्यवहार के कारण कई पायथन विशेषज्ञ लूप के else को मन में 'no break' (यदि ब्रेक नहीं हुआ) के रूप में पढ़ने की सलाह देते हैं।
2. Zero-Iteration Edge Cases and continue Behavior (शून्य-चक्र (Zero-Iteration) और continue का प्रभाव)
[EN] A subtle exam question involves empty iterables or initially false while conditions: for example, for x in []: ... else: runs the else block immediately! Because zero iterations completed naturally without executing break, Python treats the loop as having terminated normally, so else always executes. Similarly, executing continue inside the loop body skips only the rest of that cycle and never suppresses the else clause once the loop finishes. Indentation matters critically: else must align vertically with the for or while keyword, not with an inner if statement.
[HI] एक महत्वपूर्ण परीक्षा प्रश्न खाली इटरेबल या शुरू से ही असत्य while शर्त का होता है: जैसे for x in []: ... else: में else ब्लॉक तुरंत चलता है! चूँकि शून्य चक्र भी बिना किसी break के स्वाभाविक रूप से समाप्त हुए हैं, इसलिए पायथन में else निष्पादित होता है। इसी प्रकार लूप के भीतर continue चलाने पर भी केवल वर्तमान चक्र छूटता है, लूप पूरा होने पर else ब्लॉक अवश्य चलता है। इंडेंटेशन का विशेष ध्यान रखना होता है: लूप का else ठीक for या while की सीध में होना चाहिए, न कि अंदर के if की सीध में।
3. Idiomatic Search Pattern Without Flag Variables (बिना फ्लैग वेरिएबल के आदर्श सर्च पैटर्न)
[EN] Without loop-else, checking whether a target exists or whether a number is prime typically requires initializing found = False before the loop, setting found = True inside, and testing if not found afterward. With for-else, the success action (break) lives cleanly inside the if condition, while the not-found fallback lives inside else. In trial division for prime checking (for d in range(2, int(n**0.5) + 1):), finding a divisor executes break (composite), whereas exhausting all divisors enters else (prime). This eliminates state-tracking boilerplate and keeps search logic self-contained.
[HI] लूप-else के बिना किसी मान को खोजने या अभाज्य संख्या जाँचने के लिए लूप से पहले found = False सेट करना पड़ता है और लूप के बाद if not found जाँचना होता है। for-else के साथ लक्ष्य मिलने पर break चलता है, और न मिलने पर होने वाला कार्य सीधे else में लिखा जाता है। अभाज्य संख्या की जाँच (for d in range(2, int(n**0.5) + 1):) में भाजक मिलने पर break चलता है (भाज्य), और कोई भाजक न मिलने पर else चलता है (अभाज्य)। इससे अनावश्यक फ्लैग वेरिएबल नहीं बनाने पड़ते और कोड अधिक साफ रहता है।
4. Comparing Loop-else with if-else and try-except-else (लूप-else की if-else और try-except-else से तुलना)
[EN] Python reuses the else keyword across three different control structures: conditionals (if-else), loops (for/while-else), and exception handlers (try-except-else). In if-else, the else branch runs when the condition is False; in loops, else runs when no break terminated the loop; in try-except-else, else runs when no exception occurred in try. Notice the shared design philosophy in loops and try blocks: both use else to mean 'run this code if the block finished cleanly without abnormal interruption'. However, misindenting a loop's else to match an inner if statement silently alters logic from 'run once after loop' to 'run on every non-matching iteration'.
[HI] पायथन तीन अलग-अलग संरचनाओं में else कीवर्ड का उपयोग करता है: शर्तें (if-else), लूप्स (for/while-else), और एक्सेप्शन हैंडलिंग (try-except-else)। if-else में शर्त False होने पर else चलता है; लूप में break न होने पर else चलता है; और try-except-else में कोई एक्सेप्शन न आने पर else चलता है। लूप्स और try ब्लॉक में समान विचार है: यदि मुख्य ब्लॉक बिना किसी आकस्मिक रुकावट के पूरा हो, तो else चलाएं। यदि लूप का else गलती से भीतरी if के बराबर इंडेंट हो जाए, तो वह लूप के बाद एक बार चलने के बजाय हर असफल चक्र में चलने लगता है।
Syntax, Formula & Technical Example (The else Clause with Loops (for-else and while-else))
for n in (9, 13):
for d in range(2, int(n ** 0.5) + 1):
if n % d == 0:
print(n, "composite")
break
else:
print(n, "prime")
# Output:
# 9 composite
# 13 prime
The else Clause with Loops (for-else and while-else) Quick Reference & Comparison Table
| Loop Exit Condition | else Block Executes? | Explanation |
|---|---|---|
| Iterable exhausted normally | Yes (runs once) | No break occurred during iteration |
| while condition becomes False | Yes (after false check) | Natural condition termination triggers else |
| break statement executed | No (completely skipped) | Loop terminated early via break |
| Empty iterable / initially False | Yes (immediately) | Zero iterations completed without hitting break |
The else Clause with Loops (for-else and while-else) Top 20 Bilingual MCQs with Answers
Q1. When does the else block of a Python `for` loop execute?
(पायथन में `for` लूप का else ब्लॉक कब निष्पादित होता है?)
(A) Only when an exception is raised inside the loop
(B) Only when the loop finishes iterating normally without hitting break
(C) Whenever the iterable is non-empty and break runs
(D) Only when a break statement is executed inside the loop
Correct Answer: (B)
[EN] Loop else executes strictly on normal termination (no break executed).
[HI] लूप का else ब्लॉक केवल तभी चलता है जब लूप बिना किसी break के स्वाभाविक रूप से समाप्त हो।
Q2. What does this code print? for i in range(3): if i == 1: break print(i, end=' ') else: print('End')
(निम्न कोड क्या प्रिंट करेगा? for i in range(3): if i == 1: break print(i, end=' ') else: print('End'))
(A) 0
(B) 0 End
(C) End
(D) 0 1 End
Correct Answer: (A)
[EN] i=0 prints 0; at i=1, break exits the loop and skips the else block, so 'End' does not print.
[HI] i=0 पर 0 प्रिंट होता है; i=1 पर break चलने से लूप और else ब्लॉक दोनों रुक जाते हैं, अतः केवल 0 प्रिंट होता है।
Q3. What is the output of: for x in []: print('Body') else: print('Else')
(निम्न कोड का आउटपुट क्या होगा? for x in []: print('Body') else: print('Else'))
(A) Body
(B) SyntaxError
(C) Else
(D) Nothing is printed
Correct Answer: (C)
[EN] An empty iterable completes 0 iterations without hitting break, so the else clause executes.
[HI] खाली इटरेबल में शून्य चक्र बिना break के पूरे होते हैं, इसलिए else क्लॉज चलता है और Else प्रिंट होता है।
Q4. What does this while-else snippet output? i = 5 while i < 3: print('Loop') else: print('Done')
(इस while-else कोड का आउटपुट क्या होगा? i = 5 while i < 3: print('Loop') else: print('Done'))
(A) Loop
(B) Loop Done
(C) Nothing is printed
(D) Done
Correct Answer: (D)
[EN] Because i < 3 is False initially, the while body runs 0 times without break, executing else.
[HI] शुरुआत में ही i < 3 असत्य है, अतः लूप बिना break के समाप्त होता है और else से Done प्रिंट होता है।
Q5. What is printed when `continue` is used inside a for-else loop? for n in [1, 2]: if n == 1: continue else: print('OK')
(for-else लूप में `continue` चलाने पर क्या प्रिंट होगा? for n in [1, 2]: if n == 1: continue else: print('OK'))
(A) SyntaxError
(B) OK
(C) Nothing is printed
(D) 1
Correct Answer: (B)
[EN] continue skips only to the next iteration; since break never ran, the else clause still executes.
[HI] continue केवल अगले चक्र पर जाता है; चूँकि break नहीं चला, इसलिए लूप समाप्ति पर OK प्रिंट होता है।
Q6. If an exception occurs inside a `for` loop and is NOT caught inside the loop, does the loop's `else` clause run?
(यदि `for` लूप के अंदर कोई अनहैंडल्ड एक्सेप्शन आ जाए, तो क्या लूप का `else` क्लॉज चलेगा?)
(A) No; abnormal exit via exception skips the else clause
(B) Only in while loops
(C) Yes; else runs before the exception propagates
(D) Yes; only for ValueError
Correct Answer: (A)
[EN] An unhandled exception unwinds out of the loop abruptly, bypassing the loop's else suite.
[HI] अनहैंडल्ड एक्सेप्शन होने पर लूप असामान्य रूप से टूटता है और उसका else क्लॉज नहीं चलता।
Q7. If a `return` statement executes inside a loop within a function, what happens to the loop's `else` block?
(यदि किसी फंक्शन के अंदर लूप में `return` चल जाए, तो लूप के `else` ब्लॉक का क्या होता है?)
(A) It is skipped because the function exits immediately
(B) SyntaxError: return inside loop-else
(C) It executes right after return
(D) It executes before return
Correct Answer: (A)
[EN] Executing return exits the function immediately, so the loop does not complete naturally and else is skipped.
[HI] return चलते ही फंक्शन समाप्त हो जाता है, इसलिए लूप का else ब्लॉक नहीं चलता।
Q8. What is the output when `else` is aligned with `if` instead of `for`? for x in [1, 2]: if x == 2: break else: print('A', end='')
(`else` को `for` के बजाय भीतरी `if` की सीध में रखने पर क्या आउटपुट मिलेगा? for x in [1, 2]: if x == 2: break else: print('A', end=''))
(A) IndentationError
(B) AA
(C) A
(D) Nothing is printed
Correct Answer: (C)
[EN] Here else belongs to the if statement: for x=1, x!=2 so 'A' prints; for x=2, break terminates the loop.
[HI] यहाँ else अंदर वाले if से जुड़ा है: x=1 पर 'A' प्रिंट होता है और x=2 पर break से लूप समाप्त हो जाता है।
Q9. Which alternative keyword idea best captures the meaning of `else` attached to a Python loop?
(पायथन लूप से जुड़े `else` के अर्थ को कौन-सा वैकल्पिक शब्द सबसे सटीक रूप से दर्शाता है?)
(A) onerror
(B) finally
(C) always
(D) nobreak
Correct Answer: (D)
[EN] Loop-else runs specifically when no break was triggered during the loop, acting as a 'nobreak' block.
[HI] लूप-else तभी चलता है जब लूप में कोई break न चला हो, अतः यह 'nobreak' की तरह कार्य करता है।
Q10. What does this code print? i = 0 while i < 3: print(i, end='') i += 1 else: print('X')
(निम्न कोड क्या प्रिंट करेगा? i = 0 while i < 3: print(i, end='') i += 1 else: print('X'))
(A) 012X
(B) 0123X
(C) X
(D) 012
Correct Answer: (A)
[EN] The loop prints 0, 1, 2, then i becomes 3 making i < 3 False naturally, so else prints X.
[HI] लूप 0, 1, 2 प्रिंट करता है, फिर i=3 पर शर्त स्वाभाविक रूप से False हो जाती है और else से X प्रिंट होता है।
Q11. What common variable pattern does Python's for-else construct help eliminate?
(पायथन का for-else पैटर्न किस सामान्य वेरिएबल पैटर्न की आवश्यकता समाप्त कर देता है?)
(A) Accumulator sums in reduce()
(B) Try-except exception objects
(C) A boolean `found` flag initialized before a search loop
(D) Loop index counters in range()
Correct Answer: (C)
[EN] Using break when found and else when not found eliminates separate `found = False` tracking flags.
[HI] लक्ष्य मिलने पर break और न मिलने पर else का उपयोग करने से `found = False` जैसे फ्लैग वेरिएबल की आवश्यकता नहीं रहती।
Q12. Can both `for` loops and `while` loops have an `else` clause in Python?
(क्या पायथन में `for` और `while` दोनों लूप्स के साथ `else` क्लॉज हो सकता है?)
(A) Neither; else works only with if and try
(B) Only for loops support else
(C) Yes, both for and while support an optional else clause
(D) Only while loops support else
Correct Answer: (C)
[EN] Python grammar defines an optional else suite for both for_stmt and while_stmt.
[HI] पायथन व्याकरण में for और while दोनों लूप्स के लिए वैकल्पिक else क्लॉज परिभाषित है।
Q13. What does this nested loop code print? for i in range(2): for j in range(2): if j == 1: break else: print('Inner') else: print('Outer')
(निम्न नेस्टेड लूप कोड क्या प्रिंट करेगा? for i in range(2): for j in range(2): if j == 1: break else: print('Inner') else: print('Outer'))
(A) Inner only
(B) Nothing is printed
(C) Inner followed by Outer
(D) Outer
Correct Answer: (D)
[EN] The inner loop always breaks at j == 1 (skipping Inner), while the outer loop finishes normally and prints Outer.
[HI] भीतरी लूप हमेशा j==1 पर break होता है (Inner छूट जाता है), लेकिन बाहरी लूप स्वाभाविक रूप से पूरा होकर Outer प्रिंट करता है।
Q14. How many times can the `else` block of a single `while-else` loop execute per loop invocation?
(एक `while-else` लूप के चलने पर उसका `else` ब्लॉक अधिकतम कितनी बार चल सकता है?)
(A) Equal to the number of continue statements
(B) Once per iteration
(C) Twice if the condition starts False
(D) At most once
Correct Answer: (D)
[EN] A loop's else clause runs at most once after the loop finishes without breaking.
[HI] लूप बिना break के समाप्त होने पर उसका else क्लॉज अधिकतम एक बार ही चलता है।
Q15. Which Python control constructs validly accept an `else:` clause?
(पायथन की कौन-सी नियंत्रण संरचनाएँ वैध रूप से `else:` क्लॉज स्वीकार करती हैं?)
(A) for, while, and with
(B) if/elif, for, while, and try-except
(C) if/elif and def only
(D) if/elif and switch only
Correct Answer: (B)
[EN] Python supports else on conditional chains (if), loops (for/while), and exception blocks (try-except).
[HI] पायथन में शर्तों (if), लूप्स (for/while), और एक्सेप्शन ब्लॉक (try-except) के साथ else वैध है।
Q16. Which statement about Python loop `else` clauses is NOT true?
(पायथन लूप के `else` क्लॉज के बारे में निम्नलिखित में से कौन-सा कथन असत्य (NOT true) है?)
(A) The else block is skipped if break executes
(B) The else block executes only if the loop body ran at least once
(C) The else keyword aligns vertically with for or while
(D) The else block still runs if continue executes
Correct Answer: (B)
[EN] Even if the loop body runs 0 times (empty sequence or false condition), else still runs because no break occurred.
[HI] यदि लूप बॉडी एक बार भी न चले (खाली सीक्वेंस या असत्य शर्त), तब भी बिना break के समाप्ति होने पर else चलता है।
Q17. Within The else Clause with Loops (for-else and while-else), which loop exit condition directly maps to: "Yes (runs once)"?
(The else Clause with Loops (for-else and while-else) में कौन सा Loop Exit Condition सीधे "Yes (runs once)" से संबंधित है?)
(A) Iterable exhausted normally
(B) while condition becomes False
(C) Empty iterable / initially False
(D) break statement executed
Correct Answer: (A)
[EN] In The else Clause with Loops (for-else and while-else), 'Iterable exhausted normally' handles 'Yes (runs once)' (No break occurred during iteration).
[HI] The else Clause with Loops (for-else and while-else) में 'Iterable exhausted normally' का कार्य/विवरण 'Yes (runs once)' (No break occurred during iteration) है।
Q18. Which option accurately describes 'while condition becomes False' in The else Clause with Loops (for-else and while-else)?
(The else Clause with Loops (for-else and while-else) के संदर्भ में 'while condition becomes False' की सही विशेषता कौन सी है?)
(A) No (completely skipped)
(B) Yes (immediately)
(C) Yes (runs once)
(D) Yes (after false check)
Correct Answer: (D)
[EN] 'while condition becomes False' provides 'Yes (after false check)' — key point: Natural condition termination triggers else.
[HI] 'while condition becomes False' का मुख्य विवरण 'Yes (after false check)' है (Natural condition termination triggers else)।
Q19. In The else Clause with Loops (for-else and while-else), identify the correct parameter for the specification: "No (completely skipped)".
(The else Clause with Loops (for-else and while-else) में "No (completely skipped)" विवरण किस विकल्प के लिए सही है?)
(A) Iterable exhausted normally
(B) break statement executed
(C) while condition becomes False
(D) Empty iterable / initially False
Correct Answer: (B)
[EN] 'break statement executed' corresponds to 'No (completely skipped)' (Loop terminated early via break).
[HI] 'break statement executed' का संबंध 'No (completely skipped)' (Loop terminated early via break) से है।
Q20. Considering The else Clause with Loops (for-else and while-else), what key behavior defines 'Empty iterable / initially False'?
(The else Clause with Loops (for-else and while-else) में 'Empty iterable / initially False' की मुख्य भूमिका या मान क्या है?)
(A) Yes (runs once)
(B) Yes (immediately)
(C) No (completely skipped)
(D) Yes (after false check)
Correct Answer: (B)
[EN] 'Empty iterable / initially False' represents 'Yes (immediately)' (Zero iterations completed without hitting break).
[HI] 'Empty iterable / initially False' का अर्थ/मान 'Yes (immediately)' है (Zero iterations completed without hitting break)।
The else Clause with Loops (for-else and while-else) — Exam FAQs
Q1: Why does Python run the loop `else` block on an empty list?
खाली लिस्ट पर भी पायथन लूप का `else` ब्लॉक क्यों चलता है?
[EN] Because Python's loop `else` checks only one condition: did the loop terminate without executing `break`? An empty loop hits no `break`, so `else` executes.
[HI] क्योंकि लूप का `else` केवल यह देखता है कि क्या लूप बिना `break` के समाप्त हुआ। खाली लूप में `break` नहीं चलता, इसलिए `else` निष्पादित होता है।
Q2: Is it good practice to use `for-else` in production Python code?
क्या प्रोडक्शन पायथन कोड में `for-else` का उपयोग करना अच्छी प्रैक्टिस है?
[EN] Yes for concise search-and-break logic, though adding a brief comment like `# not found` above `else:` helps team members unfamiliar with Python's loop-else semantics.
[HI] सर्च-एंड-ब्रेक लॉजिक के लिए यह उपयोगी है, यद्यपि अन्य डेवलपर्स की सुविधा के लिए `else:` पर `# not found` जैसा छोटा कमेंट जोड़ना अच्छा माना जाता है।
Q3: Can a `for-else` loop also include `finally` directly without a `try` block?
क्या `try` ब्लॉक के बिना सीधे `for-else` लूप के साथ `finally` जोड़ा जा सकता है?
[EN] No. `finally` belongs exclusively to `try` statements; to guarantee cleanup around a loop, wrap the loop inside a `try...finally` block.
[HI] नहीं। `finally` केवल `try` स्टेटमेंट का हिस्सा है; लूप के आसपास क्लीनअप सुनिश्चित करने के लिए लूप को `try...finally` में रखना होता है।
Post a Comment
Post a Comment