Use a Small Test to Find a Loop Error
Expose a skipped last element with empty, one-item and ordinary lists.
Supplied materials
- Paper and pencil; an adult may read aloud. No device, account or purchase is needed.
Learn the idea
A list function needs tests for different structures as well as different numbers. Empty and one-item lists can expose loop mistakes hidden by larger examples. Our contract accepts lists of zero through four integers, and sum_all must return their sum, including the last element. An empty list’s sum is 0.
Use zero-based positions: a list of length n has positions 0 through n−1. Faulty pseudocode: total = 0; for i from 0 up to but not including n−1, add values[i]; return total. The intended endpoint is n, excluded, so every valid position is visited. Define the faulty loop as empty when its endpoint is at most 0.
- Write expected sums for [], [5] and [2, 4, 1].
- List positions visited by the faulty endpoint n−1.
- Compare actual and expected sums; note that the empty test passes.
- Repair the endpoint to n, excluded, and retest all three lists.
See the method in action
Faulty results: [] → 0; [5] → 0 because no positions are visited; [2,4,1] visits positions 0,1 and returns 6 instead of 7. Repaired endpoint n returns 0, 5 and 7 respectively.
Check your reasoning
Your answer is temporary and is not saved to an account. Download your current answer before leaving or reloading. Written work on paper or in another app stays in your own file.
See the answer and explanation
[5] with expected 5
One item is enough when it is nonzero. The faulty loop visits no positions and returns 0, while the expected sum is 5. Empty and single-zero lists pass accidentally.
Take it further
Change the test to [3, −1]. Expected correct sum: 2; faulty sum: 3 because the last −1 is skipped.
Nothing here is saved to an account or sent to a teacher. The answer download includes your current choice and any checked feedback.
Check the subject sources
These LifeStarter passages, scenarios and exercises are authored for this Academy with AI assistance and review. External resources keep their publishers’ terms. A grade label suggests a starting level; it does not establish alignment with your school’s complete requirements.