Summing Values in a Loop
Data scanning loops follow a three-step construction: initialize variables before the loop, perform computation on each item inside the loop body, and examine results after the loop completes.
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.
Initialize: Before the scan begins, set the accumulator to a starting value of 0.
Process 3: Add the current item, 3, to the accumulator. The running total becomes 3.
Process 7: Add 7 to the running total of 3. The running total becomes 10.
Process 2: Add 2 to the running total of 10. The running total becomes 12.
Process 9: Add 9 to the running total of 12. The running total becomes 21.
Process 5: Add 5 to the running total of 21. The running total becomes 26.
Examine: 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.
Start: The maximum-tracking variable begins at 3.
Read 7: Because 7 is larger than the current maximum, the tracked value changes to 7.
Read 2: Because 2 is not larger than 7, the tracked maximum remains 7.
Read 9: Because 9 is larger than 7, the tracked value changes to 9.
Read 5: Because 5 is not larger than 9, the tracked maximum remains 9.
Examine: 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 |
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
Starting the result variable inside the loop
A result-tracking variable must already have a starting value before the loop begins so that each iteration can update it.
Fix:
Initialize the result before the first item is processed.Updating the result with the wrong rule
Scanning state changes are determined by comparisons or tests of the current item.
Fix:
Define the condition that should cause a state change, then apply that condition inside the loop.Examining the result before the scan is complete
The result represents only the items processed so far until all iterations have completed.
Fix:
Examine the tracked result after the loop finishes.Assuming every scanning task updates in the same way
The general pattern is shared, but the initialization, computation, and examination depend on the task.
Fix:
Keep 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
- Initialize every result-tracking variable before the loop begins.
- Process the current item inside the loop body using the computation or test required by the task.
- Recognize that state changes may occur on every iteration, as in summing, or only when a condition is met, as in finding a maximum.
- Examine the tracked result only after the loop has completed its scan.
- Reuse 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.