Theory
menu જે એક use પછી died થઈ ગયું
છેલ્લા lesson માં, BookBridge એ તેનું menu show કર્યું, librarian એ એક book return કરી, અને program ended થઈ ગયું. next student ની book issue કરવા માટે એને scratch થી ફરી run કરવું પડ્યું.
Real menu એ દરેક action પછી પાછું આવવું જોઈએ જ્યાં સુધી એ Exit ને choose ન કરે. એ repetition છે, અને Java એના માટે 3 tools offer કરે છે: while, do-while અને for. એ ઘણીવાર each other ને imitate કરી શકે છે; exams અને clean code બંને એ one ને pick કરવા પર care કરે છે જે fit થાય.
Theory
3 kinds of chai rounds
while એ careful host છે: room માં પહેલા peeks કરે છે, chai serve કરે છે only જો કોઈ ત્યાં હોય. Possibly કોઈને serve નથી કરતું.
do-while એ confident host છે: even look કરતા પહેલા 1 cup pours કરે છે, પછી check કરે છે કે કોઈને more જોઈએ છે કે નહીં. Always ઓછામાં ઓછું એકવાર serve કરે છે.
for એ list ધરાવતો caterer છે: advance માં જાણે છે કે exactly 12 guests છે, એને one organised pass માં serve કરે છે, count કરતા જાય છે.
Formula
3 syntaxes, exam form
while (condition) { body }
do { body } while (condition);
for (initialization; condition; update) { body }
do-while ની condition પછી semicolon નો note કરો: exams એ missing કરવા માટે deduct કરે છે. for header માં: initialization once run થાય છે, condition દરેક pass પહેલાં check થાય છે, update દરેક pass પછી run થાય છે.
At a glance
right loop ને choosing
| Loop | Condition checked | Minimum runs | Best when |
|---|---|---|---|
| while | body પહેલાં | 0 | Repeat count unknown, may be zero |
| do-while | body પછી | 1 | Body ઓછામાં ઓછું એકવાર happen થવું જોઈએ (menus!) |
| for | body પહેલાં | 0 | Repeat count known (12 books print કરો) |
Practical
menu, do-while સાથે alive again
public class MenuLoop {
public static void main(String[] args) {
int choice = 0;
int round = 0;
do {
round++;
System.out.println("Round " + round + ": 1.Issue 2.Return 3.Exit");
choice = 3; // ધારો કે librarian Exit ને pick કરે છે
} while (choice != 3);
System.out.println("Goodbye");
for (int book = 1; book <= 3; book++) {
System.out.println("Book " + book + " on shelf");
}
}
}
Think first
tap કરતા પહેલા trace કરો
int i = 0; while (i < 3) { i++; } System.out.println(i); દરેક pass ને તમારા head માં work through કરો પહેલા: શું print થાય છે?
Show the answer
3. Trace: i=0, condition 0<3 true, i 1 બને છે. પછી 1<3 true, i 2 બને છે. પછી 2<3 true, i 3 બને છે. હવે check ફરી run થાય છે: 3<3 એ false છે, loop exit થાય છે, અને loop ની બહાર println 3 print કરે છે. remember કરવાનો pattern: loop variable નું final value એ first one છે જે condition ને FAILS કરે છે, last one નહીં જે pass થયું હતું.
Quiz
કયો loop તેના body ને ઓછામાં ઓછું એકવાર execute કરવાની guarantee આપે છે?
- while
- for
- do-while
- બંને 3 always ઓછામાં ઓછું એકવાર run થાય છે
Show the answer
do-while
do-while તેની condition ને body પછી check કરે છે, એટલે first pass always happens છે, જે exactly છે કે શા માટે એ menus માટે suits કરે છે: menu ને પહેલા show કરો, પછી repeat કરવું છે કે નહીં એ પૂછો. while અને for બંને body પહેલાં check કરે છે, એટલે start પર false condition એટલે 0 executions. Option D એ misconception છે જે આ question catch કરવા માટે exists કરે છે: entry-controlled loops (while, for) zero times run થઈ શકે છે.
Watch out
2 silent loop killers
forgotten update: while (i < 3) અંદર i++ વગર ક્યારેય end થતું નથી. BookBridge freeze થાય છે, fans spin કરે છે, કંઈ crash નથી થતું: infinite loop hang જેવું feel થાય છે, અને એ most common lab-exam disaster છે.
stray semicolon: while (i < 3); અથવા for (int i = 0; i < 3; i++); loop ને EMPTY body attach કરે છે. નીચેનું real block once run થાય છે, loop finish થયા પછી (અથવા ક્યારેય નહીં, જો empty loop infinite હોય).
Theory
Loops subject ના rest ને carry કરે છે
Syntax એ BCA104 ના C સાથે identical છે, એટલે આ purpose સાથે revision છે. notice કરો કે BCA204 ના Python ની પાસે શું never હતું: do-while (તમે એને while True અને break સાથે simulate કર્યું હતું). અહીંથી આગળ, loops BookBridge માં everywhere છે: Unit 3 Strings ને character by character walk કરે છે, અને Unit 5 ની linked lists node by node while (current != null) સાથે traverse કરે છે, જે just આ lesson છે object-oriented coat પહેરેલું.
Summary
Key takeaways
- while: entry-controlled, body પહેલાં check કરે છે, 0 times run થઈ શકે છે.
- do-while: exit-controlled, body પછી check કરે છે, always ઓછામાં ઓછું એકવાર run થાય છે; trailing semicolon ને remember કરો.
- for: initialization; condition; update એક header માં, known counts માટે best.
- Menus do-while want કરે છે: once show કરો, પછી repeat કરવું છે કે નહીં પૂછો.
- Infinite loops missing updates થી આવે છે; empty loops condition પછી stray semicolon થી આવે છે.
- loop variable condition ને fails કરે છે એ first value પર end થાય છે.
- Memory hook: while પહેલા peeks કરે છે, do-while પહેલા pours કરે છે, for guests ને count કરે છે.