while loop, nested while loop, break, continue

While loops एक block code को तब तक बार-बार चलाते हैं जब तक एक condition true रहती है, तुरंत बाहर निकलने के लिए break और आगे skip करने के लिए continue इस्तेमाल करते हुए।

11 min read · 12 cards · 3 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

अंतहीन Billing Counter

कल्पना कीजिए items के एक ढेर के साथ एक grocery counter पर खड़े होना। cashier एक item पकड़ता है, इसे scan करता है, इसे आपके bill में जोड़ता है, और जाँचता है कि आपकी basket में कोई items बचे हैं या नहीं। अगर items बचे हैं, cashier बिल्कुल वही sequence दोहराता है। यह cycle तब तक चलता है जब तक आपकी basket ख़ाली न हो। आपके Semester 1 BCA104 C labs में, दोहराए tasks automate करने के लिए braces के साथ सख़्त loop layouts चाहिए थे। Python इस continuous check-and-run workflow को brackets के बजाय space इस्तेमाल करके साफ़-सुथरा कैसे लागू करता है?

Theory

Guarded Metro Turnstile

एक while loop को एक smart metro turnstile gate की तरह सोचिए। जब तक आप एक positive balance वाला एक valid travel card tap करते रहते हैं, gate खुलता है और passengers को एक-एक करके जाने देता है। अगर कोई एक security alarm trigger करता है, system तुरंत lock हो जाता है, जो बिल्कुल एक break statement की तरह काम करता है। अगर एक passenger के पास एक loose coin है जो गिर जाता है, वे किनारे हट जाते हैं पर overall line को चलते रहने देते हैं, बिल्कुल एक continue statement की तरह काम करते हुए।

Theory

While Loops की संरचना औपचारिक रूप से

एक while loop एक entry-controlled iteration structure है जो एक target block code को तब तक बार-बार execute करता है जब तक एक निर्दिष्ट test condition true रहती है। Python हर execution cycle से पहले condition expression evaluate करता है। एक nested while loop multi-dimensional iteration बनाने के लिए एक loop structure को दूसरे के अंदर रखता है। इन flows को control करने के लिए, break statement loop को पूरी तरह समाप्त करता है, जबकि continue statement मौजूदा iteration के अंदर बची lines skip करता है और सीधे condition check पर वापस कूदता है।

At a glance

Table 1: Python loops के अंदर flow modifiers और control keywords।

Loop Control KeywordTrigger की गई तत्काल ActionPocketMoney Tracking उपमान
whilecondition test करता है, True होने पर block चलाता है।balance > 0 रहते transactions process करें
breakloop block से तुरंत बाहर निकलता है।card blocked होने पर तुरंत processing रोकें
continueमौजूदा pass में बची lines skip करता है।zero value entries skip करें पर list scan करते रहें

Theory

Worked Example: Smart Daily Budget Deduction

आइए अपने चलते project, PocketMoney के लिए एक interactive ledger tracking mechanism बनाएँ। हम अपने digital wallet से daily items को तब तक लगातार deduct करना चाहते हैं जब तक हमारे पास cash ख़त्म न हो, एक ख़तरनाक overdraft होने पर break इस्तेमाल करते हुए और अमान्य negative entries को पूरी तरह अनदेखा करने के लिए continue इस्तेमाल करते हुए।

Practical

PocketMoney Iterative Wallet Management

wallet_balance = 100
pending_spends = [20, -5, 40, 60, 10]
idx = 0

while idx < len(pending_spends):
    current_spend = pending_spends[idx]
    idx = idx + 1
    
    if current_spend < 0:
        continue
        
    if current_spend > wallet_balance:
        print("Stop! Insufficient funds.")
        break
        
    wallet_balance = wallet_balance - current_spend

print(wallet_balance)

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

Iteration Sequence को ट्रेस करें

ऊपर के Python script का मन में विश्लेषण कीजिए। execution पूरा होने पर wallet_balance में कौन सा बिल्कुल अंतिम balance रहेगा? tap करने से पहले array index कदम-दर-कदम ट्रेस कीजिए।

Show the answer

script output करेगी:

40

क्यों? cycles ट्रेस करते हैं। पहले, current_spend 20 है: 100 - 20 80 छोड़ता है। दूसरे, current_spend -5 है: negative check continue trigger करता है, deduction skip करते हुए। तीसरे, current_spend 40 है: 80 - 40 40 छोड़ता है। चौथे, current_spend 60 है: चूँकि 60 > 40, यह stop warning print करता है और break trigger करता है, loop से बाहर निकलते हुए। balance 40 पर रहता है।

Quiz

क्या होता है अगर एक nested loop अपने inner block के अंदर एक 'break' statement execute करता है?

  1. यह inner loop और outer loop दोनों को एक साथ समाप्त करता है।
  2. यह केवल तत्काल inner loop को समाप्त करता है, execution को वापस outer loop पर लौटाते हुए।
  3. script एक runtime NameError फेंकती है।
  4. यह अपने-आप loop counter variable को वापस शून्य पर reset करता है।
Show the answer

यह केवल तत्काल inner loop को समाप्त करता है, execution को वापस outer loop पर लौटाते हुए।

एक break keyword केवल उस innermost loop को समाप्त करने के लिए बना है जो वर्तमान में इसे रखता है। एक बार trigger होने पर, control तुरंत आस-पास के outer loop block में बाहर क़दम रखता है, जो अपने बचे cycles सामान्य रूप से चलाता रहता है।

Quiz

एक exam paper पूछता है: निम्न tracking snippet का output क्या होगा? x = 1 while x < 4: print(x, end=" ") # Missing update step

  1. 1 2 3
  2. 1 1 1 ... रोके जाने तक अनंत रूप से print करता
  3. SyntaxError: loop update step required
  4. 1 2 3 4
Show the answer

1 1 1 ... रोके जाने तक अनंत रूप से print करता

C loops के उलट जो अक्सर for(i=0;i<5;i++) जैसे संरचित headers इस्तेमाल करते हैं, Python while loops अपने-आप loop variables update नहीं करते। अगर आप अपने block के अंदर x increment करना भूल जाते हैं, condition 1 < 4 हमेशा के लिए True रहती है, एक infinite loop जाल बनाते हुए।

Watch out

Classic जाल: Variable Update Stunner

university programming labs में सबसे बार-बार marks-गँवाने वाली ग़लती एक loop के अंदर एक continue line के नीचे idx = idx + 1 जैसे loop update statement रखना है। जब Python continue से टकराता है, यह तुरंत loop के ऊपर कूदता है। यह आपकी update line को पूरी तरह skip करता है, आपकी script को एक accidental infinite loop में फँसाते हुए जो आपके university terminal को freeze कर देता है!

Theory

Loops को Semester 3 से जोड़ना

Loop controls streaming information process करने के लिए अहम हैं। Semester 3 (BCA303) में, जब आप एक external SQLite cursor object से आने वाले transactional rows पढ़ने के लिए Python इस्तेमाल करते हैं, आप data ख़त्म होने पर connection सुरक्षित रूप से समाप्त करने के लिए if not row: break जैसे एक break check के साथ मिलकर एक while True: infinite structure इस्तेमाल करेंगे।

Summary

Key takeaways

  • While loops entry controlled structures के रूप में काम करते हैं हर pass से पहले conditions जाँचते हुए।
  • Python को block के अंदर आपके loop condition variables के स्पष्ट, मैनुअल समायोजन चाहिए।
  • break statement सक्रिय loop को तुरंत तोड़ता है और अगले outer scope पर कूदता है।
  • continue statement उस cycle में बची statements skip करता है और header test पर लौटता है।
  • Nested iteration scopes को अलग, स्वतंत्र control tracking indices चाहिए।
  • Memory Hook: Breaks blocks छोड़ते हैं, continues loops फिर शुरू करते हैं!

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Conditions, Loops and Strings

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati