Mrs. Sri opens her phone in a hurry to check a customer's transfer. The PIN is wrong. Wrong again. Only the third one is right. The phone has no idea in advance how many times Mrs. Sri will mistype. It holds on to one rule: while the PIN doesn't match, ask again.
The FOR from the last lesson needs the number of rounds up front: ten lines of a times table, seven days of savings. When the number only turns up along the way, use WHILE.
WHILE ... END WHILE
Each glass of iced tea takes two ice cubes, and there are five left in the box:
ice ← 5
WHILE ice > 0 DO
PRINT "Ice cubes left:", ice
ice ← ice - 2
END WHILE
PRINT "Out of ice"
Ice cubes left: 5
Ice cubes left: 3
Ice cubes left: 1
Out of ice
- Before each round, the condition is checked. While it is TRUE, the body runs again. As soon as it is FALSE, the program carries on after
END WHILE. DOis optional.- When the condition is FALSE from the start, for example
ice ← 0, the body doesn't run even once. OnlyOut of iceis printed.
The condition has to be able to change
Try deleting the line ice ← ice - 2. The condition ice > 0 stays TRUE, and the body repeats without end. Our computer stops it after 10,000 steps, with the message "The loop on line 2 never stops: ice doesn't change inside it."
So every time you write a WHILE, look for the line inside it that changes something in its condition. If there isn't one, the loop will never stop.
Reading until a stop sign
WHILE suits input of unknown length. Mrs. Sri's cashier types the prices one by one, then 0 when done. With the input 5000, 12000, 3000, then 0:
total ← 0
READ price
WHILE price ≠ 0 DO
total ← total + price
READ price
END WHILE
PRINT "Total:", total
Total: 20000
The pattern: one READ before WHILE, so the condition has something to check. Then another READ at the end of each round, so price changes and the condition can become FALSE. If the READ inside is forgotten, price never changes, and the loop never stops.
REPEAT ... UNTIL: runs at least once
WHILE checks at the top, before a round. REPEAT checks at the bottom, after a round. The difference shows when the condition is already met at the start:
stock ← 0
WHILE stock > 0 DO
PRINT "Sell one"
stock ← stock - 1
END WHILE
PRINT "Done"
Done
stock ← 0
REPEAT
PRINT "Sell one"
stock ← stock - 1
UNTIL stock <= 0
PRINT "Done"
Sell one
Done
REPEAT sells one item while the stock is zero, because its body runs first and is checked afterwards. Look at the conditions too: WHILE says when to go on (stock > 0), UNTIL says when to stop (stock <= 0). REPEAT fits something that really must happen at least once, like asking for a PIN: there is nothing to check before someone has typed.
In JavaScript and Python, WHILE becomes while. REPEAT ... UNTIL is like do ... while in JavaScript, with the condition flipped. Python has no such form.
⚠️ Why won't the loop stop?
- A target that gets skipped. Savings go up 10000 a week, and the program waits with
WHILE savings ≠ 25000. The savings are 10000, 20000, 30000, and never exactly 25000: "The loop on line 2 never stops:savings ≠ 25000never becomes FALSE." UseWHILE savings < 25000, which stops as soon as the target is passed. - The condition is checked before it has anything in it. When
READ priceis only inside the loop,WHILE price ≠ 0stops with "Line 2:priceis used before it has a value." Put oneREADbeforeWHILE, and another at the end of its body. - The input runs out first. When the stop sign never comes, for example the cashier's input above without the
0at the end, theREADinside the loop runs out of input: "Line 5: READ needs more input, but the 3 lines of input are used up." THENon aWHILE.WHILE ice > 0 THENstops with "Line 2: pseudocode uses DO, notTHEN."THENis only forIF.
TL;DR: WHILE condition DO ... END WHILE repeats its body while the condition is TRUE. The condition is checked before each round, so the body can run 0 times. Something inside it has to change the condition, or the loop never stops and the computer halts it after 10,000 steps. For input of unknown length: one READ before WHILE, then another at the end of each round. REPEAT ... UNTIL condition checks at the bottom, so it runs at least once, and stops as soon as its condition is TRUE. To wait for a number that goes up or down, use < or >, not ≠.