Finding extremes in data
Loops scan through data by following a three-part structure: initialize a tracking variable, compute on each item, and inspect the result.
Why Scan Data
When a collection contains numbers, test scores, temperatures, or file contents, you may need to find something specific: the largest value, the smallest value, or whether a particular item exists. Checking every item manually is tedious and error-prone. A loop lets you examine each item systematically while keeping track of what you have found so far.
Finding extremes in data uses a tracking variable that records the best result found so far. As the loop progresses, the tracking variable changes only when the current item meets the condition for replacing that result.
The central mental model is not just “the loop visits every item.” It is “the loop visits every item while the tracking variable summarizes the result found so far.”
Tracking the Best So Far
Consider the generated data sequence 7, 3, 11, 9 while looking for the largest value. The first value gives the tracking variable an initial candidate. The next value is compared with that candidate. If it is not larger, the candidate stays the same. When 11 appears, it is larger than the current candidate, so the tracking variable changes. The final 9 does not replace 11 because it is not larger.
Finding the largest value
Scan the generated sequence 7, 3, 11, 9 and track the largest value found so far.
Initialize: Start the tracking variable with the first candidate, 7.
Process 3: Compare 3 with the current tracking value, 7. Because 3 is not larger, keep 7.
Process 11: Compare 11 with 7. Because 11 is larger, replace the tracking value with 11.
Process 9: Compare 9 with 11. Because 9 is not larger, keep 11.
Inspect: After every item has been processed, inspect the tracking variable.
The final tracking value is 11.
What do you think happens?
After scanning 7, 3, 11, and 9 while looking for the largest value, what remains in the tracking variable?
Reveal answer
Answer: 11
The tracking variable changes from 7 to 11 when 11 is encountered. The final value, 9, is not larger than 11, so the tracking variable remains 11.
The Three-Part Loop
Loops that search through data follow a three-part structure. First, initialize a tracking variable. Next, compute on each item as the loop progresses, usually by comparing the current item with the tracked result and updating the tracking variable when the condition is met. Finally, inspect the result after the loop has examined the collection.
| Stage | Question to ask | Purpose |
|---|---|---|
| Initialization | What value does tracking start with? | Provide the first candidate result |
| Computation | What happens for each item? | Compare the current item and update when the condition is met |
| Inspection | What is the tracking value after the scan? | Use the final result |
The three stages of a data-scanning loop
The same structure applies beyond maximum searches. To find a minimum, the replacement comparison changes so a smaller current item can become the new tracked result. Counting and summing also use a tracking variable that changes as each item is processed. The specific computation changes, but the broader pattern remains: initialize, compute in the loop, and inspect the result.
Choosing the Replacement
The current item does not automatically replace the tracking variable. Replacement depends on the condition being tested. For a maximum search, a current item replaces the tracked value when it is larger. For a minimum search, a current item replaces the tracked value when it is smaller. The comparison operator therefore determines which extreme survives the scan.
Mistakes to Avoid
Changing the tracking variable on every iteration
The tracking variable should update only when the condition for replacement is met.
Fix:
Compare the current item with the tracked result before deciding whether to replace it.Using the wrong comparison direction
The comparison operator determines whether larger or smaller values replace the current result.
Fix:
Match the comparison with the goal: retain the appropriate extreme as the scan progresses.Ignoring initialization
Proper initialization is critical to getting the right answer.
Fix:
Identify the initial candidate before tracing the loop.Inspecting the result too early
The tracked value represents the result found so far until the loop finishes.
Fix:
Inspect the tracking variable after the collection has been scanned.
Practice the Trace
A generated collection contains the values 4, 12, 6, and 15. Trace a tracking variable used to find the largest value. Record the tracking value after each item is examined, then state the final value after the loop completes.
Hints
- Begin by identifying the initial candidate.
- For each later item, compare it with the current tracked value.
- Replace the tracked value only when the current item is larger.
Tracing a second maximum search
Find the final tracked value for the generated sequence 4, 12, 6, 15.
Start with 4: The first candidate makes the tracking value 4.
Process 12: 12 is larger than 4, so the tracking value becomes 12.
Process 6: 6 is not larger than 12, so the tracking value remains 12.
Process 15: 15 is larger than 12, so the tracking value becomes 15.
Inspect: After all items are processed, inspect the final tracking value.
The final tracking value is 15.
Pattern Summary
- A data-scanning loop initializes a tracking variable, computes on each item, and inspects the result after the scan.
- The tracking variable represents the best result found so far, not necessarily the final answer until the loop completes.
- The tracking variable changes only when the current item satisfies the replacement condition.
- Initialization and the comparison direction are both critical to finding the correct maximum, minimum, count, sum, or other scanned result.
- To predict the final state, trace each item in order and record whether the tracking variable changes.
Key Takeaways
- Loops find extremes by scanning each item while maintaining a tracking variable.
- The three stages are initialization, computation during the loop, and inspection after the loop.
- A tracking variable changes only when the current item meets the comparison condition.
- Tracing the tracking variable after every item makes the final result predictable.
- The same pattern extends to maximums, minimums, counting, summing, and other data-scanning tasks.