all algorithms
algorithm 01
Loops
Repeat the same idea until the work is done. Three flavours, one metaphor: a courier with a stack of letters and a row of mailboxes.
01 / Loops
Deliver to every mailbox, in order.
n =
- i
- —
- condition
- —
- iter
- 0
- body
- 0
- delivered
- 0
step 0
- 0
def deliver_all(n): - 1
for i in range(n): - 2
deliver(i) - 3
return
Click Run to play the steps.
What you just watched
All three loop forms produce the same outcome — a delivery to every mailbox — but they make different promises about when the condition is checked. for and while check first. do-while runs the body first, then checks. Try n = 0 on the do-while variant to see the surprise.