๐ WAYPOINT LESSON
while loops and sentinels
โญ beginnerโณ 12 min read๐ Lesson 17 of 256
Pre-test loops, the four contact points, infinite loops, sentinel scans.
A while loop repeats as long as its condition is true โ checked before each pass:
int count = 1;
while (count <= 3) {
System.out.println(count);
count++; // the update MUST move toward the exit
}
// prints 1 2 3
Trace the four contact points: initialize โ test โ body โ update โ test...
Two classic failures:
- Infinite loop: the update is missing or moves the wrong way
(
count--with acount <= 3test never exits). - Off-by-one:
<vs<=decides whether the last value is processed.
A sentinel loop reads until a special value appears โ the pattern behind "read numbers until the user types -1". On the exam it appears as "process elements of an array until a negative price" style tasks:
int i = 0;
while (i < data.length && data[i] >= 0) {
total += data[i];
i++;
}
The && ordering matters: test the index bound before indexing, or the
last check reads past the end.