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 Keyword | Trigger કરેલી તાત્કાલિક Action | PocketMoney Tracking ઉપમાન |
|---|---|---|
| while | condition test કરે છે, True હોય તો block ચલાવે છે. | balance > 0 રહેતાં transactions process કરો |
| break | loop 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 કરે?
- એ inner loop અને outer loop બંનેને એક સાથે સમાપ્ત કરે છે.
- એ માત્ર તાત્કાલિક inner loop ને સમાપ્ત કરે છે, execution ને પાછું outer loop પર લઈ જતાં.
- script એક runtime NameError ફેંકે છે.
- એ આપોઆપ 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 2 3
- 1 1 1 ... રોકાય ત્યાં સુધી અનંત રીતે print કરતાં
- SyntaxError: loop update step required
- 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 ફરી શરૂ કરે છે!