Definite vs Indefinite Loops
Two kinds of loop
The for loop from earlier in the course is a definite loop: the number of passes can be worked out before the loop starts running. Ten items in a list means ten passes, and you know that before you press run.Sometimes the number of passes cannot be worked out ahead of time. That is an indefinite loop. Imagine a program that keeps reading numbers a user types until they type a stop word. How many numbers will they type? Nobody knows, including the program.
The while loop
JavaScript writes indefinite iteration with while. A while loop repeats its block for as long as a condition stays true.The condition is tested before every pass, including the very first one. If the test comes back true, the block runs and the condition is tested again. The first time it comes back false, the loop ends and the program carries on below it.
One consequence catches everybody once: if the condition is already false when the loop is reached, the block never runs at all. A while loop is perfectly happy to run zero times.
The program below pours cups from a jug for as long as there is enough water left for a full cup. Run it, then make each change below and run it again. Press Reset before each change, so that each one starts from the original program.
• In
let water = 50;, change 50 to 5.• In
let cupSize = 7;, change 7 to 15.• Change the condition to
water > 0.let water = 50;
let cupSize = 7;
let cups = 0;
while (water >= cupSize) {
water = water - cupSize;
cups = cups + 1;
console.log("Cup", cups, "poured,", water, "left");
}
console.log("Cups poured:", cups);
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.
Choosing between for and while
Ask one question: can you state the number of passes before the loop starts? If yes, that is definite iteration and a for loop says so plainly. If no, reach for while.| The job | Pass count known first? | Reach for |
|---|---|---|
| Print a label for each of 40 seats | Yes, 40 of them | for |
| Keep rolling a die until a six comes up | No | while |
| Add up the first 12 entries of a list | Yes, 12 of them | for |
| Keep doubling a savings balance until it passes a target | Not by eye | while |
The last row is worth a second look. The pass count is not obvious, but it is not unknowable either, and either loop could be made to work. When you are on the fence, while is the honest choice, because it does not promise a count you cannot supply.
do...while: the test at the bottom
A do...while loop puts the test after the block instead of before it. The block therefore always runs at least once, no matter what the condition says.Leaving early: break and continue
Two statements interrupt the normal rhythm of a loop, and they are not the same.• break leaves the loop immediately. The rest of the block is skipped and the condition is never tested again.
• continue abandons only the current pass. Control jumps back to the condition and the loop keeps going.
The program below parks cars in spaces 1 to 12, skips a closed space with
continue, and stops with break once every car is parked. Run it, then make each change below and run it again. Press Reset before each change.• Change
continue to break.• In
let cars = 6;, change 6 to 20.• Cut the line
space = space + 1; and paste it as the last line inside the loop, below the closing brace of the second if. This version never finishes, so after a few seconds the runner stops it and says it ran too long.let cars = 6;
let space = 0;
while (space < 12) {
space = space + 1;
if (space === 5) {
continue; // this space is closed
}
console.log("Car parked in space", space);
cars = cars - 1;
if (cars === 0) {
break; // every car is parked
}
}
console.log("Last space checked:", space);
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.