LOOPSChapter 3 · Loops and iteration
Four loop patterns
Most loops follow one of a few patterns. An accumulator starts at 0 and adds as it goes. A counter adds 1 when something matches. A tracker remembers the best value seen so far. A collector starts with an empty list and appends. Learn these four shapes and most loop problems become fill in the blanks.
Worked example
How it reads
- Accumulator:
total += hadds every value - Counter and collector:
tall += 1andbig.append(h)only when the condition holds - Tracker: start with the first item, replace it when something beats it

Cloud tip: Start a tracker with the first item, not with 0. If every value is negative, starting at 0 gives the wrong answer.


