Finding the Maximum Value
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 locate the largest number, find the smallest value, or count items that meet a condition. A loop avoids writing separate instructions for each item and provides a repeatable way to examine the data.
A data scanning loop has three stages: initialize tracking variables before the loop, compute with each item inside the loop, and examine the result after the loop.
The Three-Stage Pattern
For maximum-finding, the tracking variable represents the largest value found so far. Before scanning begins, it receives a starting value. During each iteration, the current item is compared with that tracked value. If the current item is larger, the tracking variable is updated. If it is not larger, the tracking variable stays as it is. After every item has been processed, the final tracking value is examined as the result.
Tracing the Current Maximum
Consider the source trace in which max_value starts at 3 and the scan encounters values 7, 2, 9, and 5. The important question at each step is not simply which item is being visited. It is whether that item should replace the current maximum.
| Item examined | Current maximum after the comparison | State change |
|---|---|---|
| Starting value | 3 | Initialized |
| 7 | 7 | Changed because 7 is larger than 3 |
| 2 | 7 | Stayed the same because 2 is not larger than 7 |
| 9 | 9 | Changed because 9 is larger than 7 |
| 5 | 9 | Stayed the same because 5 is not larger than 9 |
The tracking variable changes only when the incoming item is larger than the current maximum.
This trace shows selective updating. The loop examines every item, but max_value changes only twice: first from 3 to 7, and then from 7 to 9. The smaller values do not cause an update. Once the loop completes, the final value, 9, is examined as the result.
The Update Decision
The update decision is the central mechanism of the pattern. The current item and the tracked maximum are compared inside the loop. A larger current item causes a state transition: the tracked maximum becomes that item. A current item that is not larger causes no state transition: the tracked maximum remains unchanged. Therefore, the loop both scans continuously and updates selectively.
Do not expect the tracking variable to change on every iteration. Visiting an item and changing the state are different events.
Initialization and Final Examination
The maximum-tracking variable has two especially important moments. Before the loop, it is initialized so a comparison can take place. After the loop, it is examined because all items have then been processed. The source trace begins with max_value at 3 and ends with max_value at 9 after the larger values have replaced the earlier state.
Initializing the tracking variable inside the loop
The variable needs a starting value before the loop and must retain updates from earlier iterations.
Fix:
Initialize the result-tracking variable before scanning begins.Updating the maximum for every item
State should change only when the comparison condition is met.
Fix:
Replace the current maximum only when the current item is larger.Examining the result before the loop finishes
The final result is available only after every item has been processed.
Fix:
Examine the tracking variable after the loop completes.
Adapting the Pattern
Finding a maximum is one use of a broader scanning pattern. The three stages remain in place when the goal changes: initialize before the loop, compute during each iteration, and examine after the loop. What changes is the tracking variable's starting value, the computation performed for each item, and the result examined at the end. This same structure supports finding a minimum, summing values, counting items, or searching for a specific value.
| Scanning task | What changes | What remains constant |
|---|---|---|
| Find a maximum | Track and conditionally replace the largest value | Initialize, process each item, examine after the loop |
| Find a minimum | Track and conditionally replace the smallest value | Initialize, process each item, examine after the loop |
| Sum values | Update a running total for each item | Initialize, process each item, examine after the loop |
| Count items | Update a count when the relevant condition is met | Initialize, process each item, examine after the loop |
Different scanning tasks vary in their initialization and update work, but share the same three-stage construction.
Plan a scanning loop for finding the minimum value. Identify what must be initialized before the loop, what comparison or computation occurs for each item, and when the final result is examined.
Hints
- Start by naming the result-tracking variable.
- Decide what condition would justify replacing its current value.
- Place the final examination after every item has been processed.
Reliable Scanning
- Initialize result-tracking variables before the loop so they have a starting value.
- Process each item inside the loop and use a comparison or test to decide whether the state changes.
- The current maximum changes only when the incoming item is larger; otherwise it remains unchanged.
- Examine the final tracking value after the loop has completed.
- The same initialize, compute, and examine pattern applies to maximums, minimums, sums, counts, and other scanning tasks.
Key Takeaways
- A data scanning loop initializes tracking variables before the loop, computes with each item during the loop, and examines the result afterward.
- Finding a maximum depends on comparing each incoming item with the current maximum.
- The tracking state changes only when the comparison condition is satisfied.
- A loop can examine every item while changing the tracked result only when necessary.
- The same structure can be adapted to finding minimums, summing, counting, and other scanning tasks.