Unit 5: Loops and Loop Invariants
Repetition that stops on its own, the property that stays true every pass, and the ways a loop runs forever by mistake.
6 topics · 21 questions
Each topic is a path, not a menu: work down it in order and every question builds on the one above. Click any of them to watch the answer draw itself.
The while Loop
- How it worksHow does a while loop re-check its condition after every single pass through the body?
- Worked exampleTrace count = 0 in a while count < 3 loop that prints count then adds 1.
- Common mistakeWhy doesn't a while loop check its condition only once, at the very start?
Your turn1 of 2
A while loop starts count at 0 and runs while count < 70, printing count then adding 1 each pass. How many times does the loop body run?The for Loop and Counted Iteration
- How it worksHow does a for loop pull one item at a time from a range or a list on every pass?
- Worked exampleTrace what for i in range(5): print(i·2) prints on each pass.
- Common mistakeWhy doesn't range(5) include the number 5 itself?
Your turn1 of 2
A for loop runs for i in range(697), pulling one item at a time. How many total passes does the loop make?Loop Invariants: What Stays True on Every Pass
- How it worksHow does a loop invariant stay true before the loop starts, after every pass, and once it ends?
- Worked exampleTrace a running-total loop over 1, 2, 3 and state the invariant that holds after each addition.
- Common mistakeWhy doesn't a loop invariant need to describe the loop's final answer, only what stays true along the way?
- Going furtherWhat happens to a loop's correctness argument once its invariant is violated on one single pass?
- Ties togetherHow does proving a loop invariant resemble proving the base case and step of an induction argument?
Your turn1 of 2
A running-total loop over 35 numbers each equal to 20 maintains the invariant that the total equals v times the number of passes completed so far. After all n passes, what does the total equal?Infinite Loops and Off-by-One Errors
- How it worksHow does forgetting to update a loop's counter leave its condition true on every pass forever?
- Worked exampleTrace why for i in range(1, 11) prints ten numbers when a coder expecting eleven miscounted by one.
- Common mistakeWhy isn't an off-by-one error just a rare, unusual kind of mistake to worry about?
Your turn1 of 2
A coder mistakenly expects range(1, 71) to print n numbers total, but range excludes its endpoint. How many numbers does range(1, 71) actually print?break and continue
- How it worksHow does break exit a loop immediately while continue only skips ahead to the next pass?
- Worked exampleTrace a loop over 1 through 6 that uses continue to skip even numbers and break once it hits 5.
- Going furtherWhat changes in a nested loop when break only exits the innermost loop and not the outer one?
Your turn1 of 2
A loop uses continue to skip 6 of its 73 total passes, and processes the rest normally before finishing without ever breaking. How many passes does the loop's body actually execute code for?The Accumulator Pattern
- How it worksHow does an accumulator variable carry a running result forward from one pass of a loop to the next?
- Worked exampleTrace total = 0 accumulating the sum of 4, 7, and 2 one pass at a time.
- Worked exampleTrace count = 0 accumulating how many of 3, -1, 5, -2, 8 are positive.
- Common mistakeWhy doesn't an accumulator need to be reset inside the loop on every single pass?
Your turn1 of 2
An accumulator variable total starts at 0. A loop adds 35, 33, and 47 to it, one at a time. What value does total hold at the end?