Theory
The menu that died after one use
Last lesson, BookBridge showed its menu, the librarian returned one book, and the program ended. To issue the next student's book she had to run it again from scratch.
A real menu must come back after every action until she chooses Exit. That is repetition, and Java offers 3 tools for it: while, do-while and for. They can often imitate each other; exams and clean code both care about picking the one that fits.
Theory
3 kinds of chai rounds
while is the careful host: peeks into the room first, serves chai only if someone is there. Possibly serves nobody.
do-while is the confident host: pours 1 cup before even looking, then checks whether anyone else wants more. Always serves at least once.
for is the caterer with a list: knows in advance there are exactly 12 guests, serves them in one organised pass, counting as it goes.
Formula
The 3 syntaxes, exam form
while (condition) { body }
do { body } while (condition);
for (initialization; condition; update) { body }
Note the semicolon after do-while's condition: exams deduct for missing it. In the for header: initialization runs once, the condition is checked before every pass, the update runs after every pass.
At a glance
Choosing the right loop
| Loop | Condition checked | Minimum runs | Best when |
|---|---|---|---|
| while | Before the body | 0 | Repeat count unknown, may be zero |
| do-while | After the body | 1 | Body must happen at least once (menus!) |
| for | Before the body | 0 | Repeat count known (print 12 books) |
Practical
The menu, alive again with do-while
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; // pretend the librarian picks Exit
} while (choice != 3);
System.out.println("Goodbye");
for (int book = 1; book <= 3; book++) {
System.out.println("Book " + book + " on shelf");
}
}
}
Think first
Trace it before you tap
int i = 0; while (i < 3) { i++; } System.out.println(i); Work through every pass in your head first: what prints?
Show the answer
3. Trace: i=0, condition 0<3 true, i becomes 1. Then 1<3 true, i becomes 2. Then 2<3 true, i becomes 3. Now the check runs again: 3<3 is false, the loop exits, and the println outside the loop prints 3. The pattern to remember: the loop variable's final value is the first one that FAILS the condition, not the last one that passed it.
Quiz
Which loop is guaranteed to execute its body at least once?
- while
- for
- do-while
- All 3 always run at least once
Show the answer
do-while
do-while checks its condition AFTER the body, so the first pass always happens, which is exactly why it suits menus: show the menu first, ask afterwards whether to repeat. while and for both check BEFORE the body, so a false condition at the start means 0 executions. Option D is the misconception this question exists to catch: entry-controlled loops (while, for) can run zero times.
Watch out
The 2 silent loop killers
The forgotten update: while (i < 3) with no i++ inside never ends. BookBridge freezes, fans spin, nothing crashes: an infinite loop feels like a hang, and it is the most common lab-exam disaster.
The stray semicolon: while (i < 3); or for (int i = 0; i < 3; i++); attaches an EMPTY body to the loop. The real block below runs once, after the loop finishes (or never, if the empty loop is infinite).
Theory
Loops carry the rest of the subject
Syntax is identical to BCA104's C, so this is revision with a purpose. Notice what BCA204's Python never had: a do-while (you simulated it with while True and break). From here on, loops are everywhere in BookBridge: Unit 3 walks along Strings character by character, and Unit 5's linked lists traverse node by node with while (current != null), which is just this lesson wearing an object-oriented coat.
Summary
Key takeaways
- while: entry-controlled, checks before the body, may run 0 times.
- do-while: exit-controlled, checks after the body, always runs at least once; remember the trailing semicolon.
- for: initialization; condition; update in one header, best for known counts.
- Menus want do-while: show once, then ask whether to repeat.
- Infinite loops come from missing updates; empty loops come from a stray semicolon after the condition.
- A loop variable ends at the first value that fails the condition.
- Memory hook: while peeks first, do-while pours first, for counts the guests.