Study guide: Combine Loops and Function Inputs
Trace loop scope, nested repetitions, two accumulators and independent function calls.
Ideas to explain in your own words
- Trace a loop by keeping a clear record of changing values.
- Distinguish outer iterations, inner iterations and initialization scope.
- Use two accumulators to describe selected list values.
- Substitute parameters, follow a local loop and return its result.
Watch your reasoning
- The trace is 1 → 3 → 5 → 7 → 9. Four additions of 2 contribute 8, and the starting 1 is still included.
- There are 3 × 2 = 6 inner visits, each adding 2; the total is 12.
- Only 2 and 5 satisfy x > 0. Their count is 2 and sum is 7; zero and −1 change neither.
- The trace is 1 → 3 → 5 → 7. The starting 1 plus three additions of 2 gives 7.
A worked review example
Follow a repeated instruction: Before the loop total is 1. After repetitions 1, 2 and 3 it is 3, 5 and 7. The shown answer is 7; the loop added 2 three times. Trace a Repeat Inside a Repeat: The six rows end with totals 1, 2, 3, 4, 5, 6. Shown result: 6. With total = 0 moved just inside the outer loop, each outer group ends at 3 and the final result is 3. Keep a Count and a Sum: After x = 4: (count, total) = (1, 4). After 0 and −2: still (1, 4). After 3: (2, 7). Returned pair is (2, 7), not (7, 2). Trace a Function Call Separately: bump(2, 3, 2): value 2 → 5 → 8, return 8. bump(5, 1, 0): value starts at 5, no loop visits occur, return 5. The second call does not start at 8.
Make a practice plan
- Try the supplied lesson problems without looking at the answers.
- Compare your method with the worked examples and explain any correction.
- Use the new review cases, then complete the transfer task on paper or in your own document.