Theory
મળી ગયું. શોધતા કેમ રહીએ?
Marks program 60 records માં roll number 42 શોધે છે. Loop એને position 7 પર મેળવી લે છે... અને પછી પ્રામાણિકપણે records 8 થી 60 સુધી તપાસે છે, કંઈ શીખ્યા વગર.
બીજો દિવસ: class નું average કાઢવું, પણ absent students -1 થી ચિહ્નિત છે, અને એ total ને બગાડવા ન જોઈએ.
બે અલગ જરૂરિયાત: loop ને પૂરી રીતે છોડવું, અને ફક્ત આ round છોડવો. C બંને આપે છે, સાથે એક જંગલી પૂર્વજ જેને એના પિંજરામાં જ રાખવો સારો.
Theory
કતાર છોડવી સામે એક counter છોડવો
તમે counters ની એક કતાર પર ચાલો છો. break એ hall માંથી બહાર નીકળી જવું છે: જે પણ counters બચ્યા હોય, તમે એ બધાથી પરવારી ગયા. continue એ એક counter છોડવો છે ("આ બંધ છે, આગળ વધો") બાકી માટે કતારમાં રહેતાં. અને goto એ એક label બૂમ પાડીને ઇમારતમાં ગમે ત્યાં teleport થવું છે, રોમાંચક, અને બરાબર એ રીતે ઇમારતો અંધાધૂંધીમાં ઊતરે છે.
Theory
ત્રણેય, ઔપચારિક રીતે
- break; enclosing loop (કે switch) ને તરત પૂરો કરે છે; execution એના closing brace પછીથી ફરી શરૂ થાય છે. Nested loops માં એ ફક્ત એક level છોડે છે.
- continue; હાલના iteration નો બાકીનો ભાગ છોડી દે છે અને આગળના round પર કૂદે છે. એક
forloop માં, update (i++) તોય ચાલે છે, પછી condition test થાય છે. - goto label; function માં ગમે ત્યાં
label:પર બિનશરતી કૂદે છે. Legal C, પણ એ sequence/selection/iteration શિસ્ત ને spaghetti માં ઓગાળી નાખે છે; આધુનિક code એને લગભગ ક્યારેય allow કરતું નથી (એક ક્લાસિક માફી: deep nesting થી બચવું).
Practical
break શોધે છે, continue ગાળે છે
#include <stdio.h>
int main() {
int marks[6] = {70, -1, 55, 90, -1, 62};
int i, total = 0, present = 0;
for (i = 0; i < 6; i++) {
if (marks[i] == -1)
continue; /* absent: skip this round */
total += marks[i];
present++;
}
printf("Average of %d students: %d\n", present, total / present);
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Think first
Filter ને trace કરો
ઉપરના loop પર ચાલો. કયા indexes total += સુધી પહોંચે છે, અંતે total અને present શું છે, અને શું print થાય છે?
Show the answer
Indexes 1 અને 4 continue થી ટકરાય છે અને કંઈ યોગદાન આપતા નથી. Indexes 0, 2, 3, 5, 70 + 55 + 90 + 62 ઉમેરે છે: total = 277, present = 4, print થાય છે Average of 4 students: 69 (integer division).
ધ્યાન આપો continue એ શું ન કર્યું: એણે ક્યારેય loop પૂરો ન કર્યો; એણે ફક્ત absentees ને fast-forward કર્યા. એ જ ફરક આખો exam સવાલ છે.
Quiz
10 items process કરતા એક loop ની અંદર, item 4 પર break થી ટકરાવા અને continue થી ટકરાવા માં શું ફરક છે?
- break, items 4-10 ને પૂરી રીતે છોડી દે છે; continue, item 4 છોડે છે અને 5-10 process કરે છે
- બંને loop રોકી દે છે, break બસ ઝડપથી કરે છે
- continue loop માંથી બહાર નીકળે છે; break એક item છોડે છે
- break, loop ને item 1 થી ફરી શરૂ કરે છે
Show the answer
break, items 4-10 ને પૂરી રીતે છોડી દે છે; continue, item 4 છોડે છે અને 5-10 process કરે છે
break એ બહાર છે: loop પૂરો, items 5 થી 10 ક્યારેય થતા નથી. continue એ આગળનું છે: ફક્ત round 4 નું બાકી છોડાય છે, અને loop જીવતો રહે છે. Option C એમને ઉલટાવે છે, પ્રમાણભૂત trap. એક-line યાદ: break = hall છોડો, continue = એક counter છોડો.
Quiz
એક break, nested જોડી ના INNER loop ની અંદર execute થાય છે. શું અટકે છે?
- ફક્ત inner loop; outer પોતાનું આગળનું iteration ચાલુ રાખે છે
- બંને loops એકસાથે અટકી જાય છે
- ફક્ત outer loop અટકે છે
- આખું program પૂરું થઈ જાય છે
Show the answer
ફક્ત inner loop; outer પોતાનું આગળનું iteration ચાલુ રાખે છે
break બરાબર એક level છોડે છે: એ loop જે સીધો એને ઘેરે છે. Outer loop ને ક્યારેય ખબર પડતી નથી અને એ પોતાના આગળના round સાથે આગળ વધે છે. બંને levels માંથી નીકળવા માટે એક flag variable જોઈએ (કે, એ એક ક્લાસિક માફ કરેલો goto). "શું break બંને nested loops માંથી નીકળે છે?" એ ભરોસાપાત્ર exam યુક્તિ છે, અને જવાબ ના છે.
Watch out
Marks ક્યાં કપાય છે
Trace સવાલોમાં break અને continue ઉલટાવવા, ધીમે ચાલો અને દરેકને label કરો. એમ માનવું કે break બધા nested levels માંથી નીકળે છે: ફક્ત એક level. એમ લખવું કે continue, for નું update છોડે છે: i++ તોય ચાલે છે, એટલે for loop માં continue ભાગ્યે જ લટકે છે, પણ એક while loop માં manual i++ ની પહેલાં મૂકેલો continue હંમેશા માટે loop કરી શકે છે, એક સૂક્ષ્મ પ્રિય. અને exams માં, goto ને "legal પણ બહિષ્કૃત: એ spaghetti code બનાવે છે" કહો.
Theory
Unit 2 બંધ થાય છે
નિર્ણયો, loops, nesting, અને હવે કટોકટીના exits: marks program પોતાના flow ને પૂરી રીતે નિયંત્રિત કરે છે. જે હજી એની પાસે નથી એ એક ઠીકઠાક MEMORY છે: 60 marks ને અત્યારે 60 variables જોઈએ. Unit 3 એને એક ઝાટકે ઠીક કરે છે arrays થી, એક નામ, 60 numbered boxes, અને તમે જે દરેક loop શીખ્યા છો એ દસ ગણો વધુ ઉપયોગી થઈ જાય છે.
Summary
Key takeaways
- break: enclosing loop કે switch ને તરત છોડો, ફક્ત એક nested level.
- continue: આ round નું બાકી છોડો; loop (અને for નું update) ચાલતું રહે છે.
- goto label: બિનશરતી કૂદ; legal, પણ spaghetti, બહિષ્કૃત.
- ઉપયોગ: break એક સફળ search પૂરું કરે છે; continue ખરાબ records ગાળે છે.
- while loop ના manual increment ની પહેલાં continue લટકાવી શકે, for loops સલામત છે.
- યાદ રાખવાની યુક્તિ: hall છોડો (break), એક counter છોડો (continue).