Summing Values in a Loop
A data-scanning loop has three stages: initialize before the loop, compute during each iteration, and examine the result after the loop.
Try it: Loop Tracer
How a Python for loop visits each item in turn, and how an accumulator variable (a total, a count, a best-so-far or a result list) changes on every iteration.
How it works
- Initialise the accumulator before the loop.
- Each iteration, the loop variable takes the next value from the list.
- An if inside the loop decides whether to update the accumulator.
- After the last item the loop ends and the accumulator holds the answer.
Default run (13 steps): values = [4, 9, 2, 7, 5]. Run the loop one statement at a time. … The loop has used every value. print(total) shows 27.
Simplified: A model of five fixed loop programs (sum, count, maximum, minimum, filter) run on your list — it steps the program exactly as Python would, but it does not run Python.
Loading the simulation…
Why Scan Every Item
Many programming tasks require examining every item in a collection, such as a list, a file, or a sequence of user inputs. The task may be to find a largest or smallest value, count items that meet a condition, or combine values into a total. A loop avoids writing separate instructions for each item and gives these tasks a predictable structure.
A data-scanning loop has three stages: initialize before the loop, compute during each iteration, and examine the result after the loop.
The Three-Stage Pattern
For a summing loop, the result variable is an accumulator: it stores the running total. First, initialize the accumulator before the loop so it has a starting value and is ready to be updated. Next, process the current item inside the loop body by adding that item to the accumulator. Finally, examine the accumulator after the loop completes, when all items have been processed.
Keep the three stages conceptually separate. If the starting value is missing, the accumulator is not ready. If the update is missing, the scan does not build a result. If the result is examined too early, it does not yet represent the complete data set.
Tracing a Running Total
Summing five scanned values
Scan the values 3, 7, 2, 9, and 5 and maintain a running total.
- 1Initialize
Before the scan begins, set the accumulator to a starting value of 0.
- 2Process 3
Add the current item, 3, to the accumulator. The running total becomes 3.
- 3Process 7
Add 7 to the running total of 3. The running total becomes 10.
- 4Process 2
Add 2 to the running total of 10. The running total becomes 12.
- 5Process 9
Add 9 to the running total of 12. The running total becomes 21.
- 6Process 5
Add 5 to the running total of 21. The running total becomes 26.
- 7Examine
After every item has been processed, examine the accumulator. It contains the total for all five values.
The final running total is 26.
| Position | Current item | Accumulator before | Accumulator after |
|---|---|---|---|
| 1 | 3 | 0 | 3 |
| 2 | 7 | 3 | 10 |
| 3 | 2 | 10 | 12 |
| 4 | 9 | 12 | 21 |
| 5 | 5 | 21 | 26 |
Each row represents one loop iteration in the generated summing example.
Selective State Changes
The accumulator in a sum is updated for each item because each item contributes to the total. Other scanning tasks update their tracked state only when a condition is met. For example, in a maximum-finding loop, the current item is compared with the current maximum. The state changes when the current item is larger; otherwise, the current maximum remains unchanged.
Comparing a maximum scan with a sum scan
Trace the source's maximum-finding values: 3, 7, 2, 9, and 5.
- 1Start
The maximum-tracking variable begins at 3.
- 2Read 7
Because 7 is larger than the current maximum, the tracked value changes to 7.
- 3Read 2
Because 2 is not larger than 7, the tracked maximum remains 7.
- 4Read 9
Because 9 is larger than 7, the tracked value changes to 9.
- 5Read 5
Because 5 is not larger than 9, the tracked maximum remains 9.
- 6Examine
After the scan, examine the tracked maximum. The final value is 9.
A scanning loop can visit every item while changing its state only when the task's condition is satisfied.
From Summing to Searching
Summing and searching use the same overall scanning shape. Both initialize tracking variables before the loop, inspect items during the loop, and examine the tracked result after the loop. What changes is the computation: summing updates a running total, while searching tests the current item against a target and updates the search state when the required condition is met.
| Scanning task | Initialize | Inside the loop | After the loop |
|---|---|---|---|
| Summing | A starting total | Combine the current item with the total | Examine the final total |
| Finding a maximum | A starting maximum | Compare the current item and update when it is larger | Examine the final maximum |
| Finding a minimum | A starting minimum | Compare the current item and update when it is smaller | Examine the final minimum |
| Counting | A starting count | Update when the item meets the condition | Examine the final count |
| Searching for a specific value | Search-tracking state | Test the current item against the target | Examine the search result |
The three-stage structure stays consistent while the initialization, update rule, and final examination change.
When designing a new scanning loop, state the three choices explicitly: What starting state is needed? What should happen for the current item? What result should be examined after the final item?
Mistakes in Scanning Loops
Common mistake Starting the result variable inside the loop
What it looks likeThe tracked result is given its starting value only after scanning has begun.
Why it failsA result-tracking variable must already have a starting value before the loop begins so that each iteration can update it.
Do this insteadInitialize the result before the first item is processed.
Common mistake Updating the result with the wrong rule
What it looks likeA maximum-finding task changes the result for every item instead of comparing the current item with the current maximum.
Why it failsScanning state changes are determined by comparisons or tests of the current item.
Do this insteadDefine the condition that should cause a state change, then apply that condition inside the loop.
Common mistake Examining the result before the scan is complete
What it looks likeTreating an intermediate accumulator value as the final answer.
Why it failsThe result represents only the items processed so far until all iterations have completed.
Do this insteadExamine the tracked result after the loop finishes.
Common mistake Assuming every scanning task updates in the same way
What it looks likeUsing the update rule for summing when the task is finding a maximum or searching for a target.
Why it failsThe general pattern is shared, but the initialization, computation, and examination depend on the task.
Do this insteadKeep the three stages and change the task-specific update rule.
Design Practice
Design the three stages of a scanning loop that examines a sequence of values and counts how many items meet a chosen condition. State what is initialized before the loop, what test and update occur for each item, and what is examined after the loop.
Hints
- Begin with a count that is ready to be updated before the first item.
- Inside the loop, test the current item against the condition.
- Update the count only when the condition is met, then examine the final count after all items have been scanned.
Trace a sum accumulator for the values 4, 1, and 6. Write the accumulator value after initialization and after each item is processed.
Hints
- Use a starting total of 0 for this generated exercise.
- Add one current item at a time.
- Do not report the final result until every item has been processed.
Reliable Loop Design
- 1Initialize every result-tracking variable before the loop begins.
- 2Process the current item inside the loop body using the computation or test required by the task.
- 3Recognize that state changes may occur on every iteration, as in summing, or only when a condition is met, as in finding a maximum.
- 4Examine the tracked result only after the loop has completed its scan.
- 5Reuse the three-stage pattern for summing, counting, finding maximums or minimums, and searching for a specific value.
Key Takeaways
- A data-scanning loop follows the sequence initialize, process, and examine.
- A sum loop maintains an accumulator that changes as each item contributes to the running total.
- Other scanning loops may update their state conditionally when the current item satisfies a comparison or test.
- The same overall structure supports summing, counting, finding maximums or minimums, and searching for a specific value.
- The final tracked result should be examined after all items have been processed.