Basic while loop syntax
Loops scan through data by following a three-part structure: initialize a tracking variable, compute on each item, and inspect the result.
Why Tracking Matters
When a collection contains numbers, test scores, temperatures, or file contents, you may need to find something specific, such as the largest value, the smallest value, or whether a particular item exists. Checking every item manually is tedious and error-prone. A loop provides a systematic way to examine the data while keeping track of what has been found so far.
The central idea is not merely repeating an action. A data-scanning loop maintains a tracking variable. That variable represents the current result while the loop progresses through the collection. Its state may change when the current item satisfies the comparison being used, and it remains unchanged when the condition is not met.
The Three-Part Pattern
Loops that scan data follow a three-part structure: initialize a tracking variable, compute on each item, and inspect the result. Initialization establishes the starting state. Computation compares or processes the current item during the loop. Inspection happens after the scan, when you use the tracking variable as the result.
Before examining the loop's repeated work, identify the meaning of the tracking variable and its initial value. Then identify the comparison that can change it. This makes the loop's result easier to predict.
A Maximum-Value Trace
Consider a generated scan of the collection 7, 3, 11, and 5. Suppose the tracking variable represents the largest value seen so far and begins at 7. The loop examines each remaining item in sequence. When an item is larger than the current tracking value, the tracking variable changes; otherwise, it stays as it is.
| Point in the scan | Current item | Tracking value | Reason |
|---|---|---|---|
| Initial state | 7 | 7 | The tracking variable starts with the first value |
| After examining 3 | 3 | 7 | 3 does not exceed the current tracking value |
| After examining 11 | 11 | 11 | 11 exceeds the current tracking value, so the result changes |
| After examining 5 | 5 | 11 | 5 does not exceed the current tracking value |
Generated trace of a tracking variable used to find a maximum
What do you think happens?
After the loop examines 7, 3, 11, and 5, what will the tracking variable contain if it stores the largest value seen so far?
Reveal answer
Answer: 11
The tracking value begins at 7, remains 7 after examining 3, changes to 11 when that larger value is found, and remains 11 after examining 5.
Reading the Final State
The final state is the tracking variable's value after every item has been considered. In the generated maximum-value trace, the initial value is 7 and the final value is 11. The important point is that the variable does not change on every iteration. It changes only when the current item satisfies the comparison, so the final value depends on both the initial value and the comparison rule.
To predict a final state, trace the tracking value one item at a time. Do not assume that the last item becomes the result. The last item changes the result only if it satisfies the comparison. This same reasoning applies when the task is finding a minimum, counting matching items, summing values, or checking whether a particular item exists.
Common Scanning Mistakes
Choosing an unsuitable initial tracking value
The loop's later decisions depend on the value already stored in the tracking variable.
Fix:
Check what the tracking variable is meant to represent before selecting its starting state.Using the wrong comparison operator
The variable may change when it should remain unchanged, or fail to change when a better candidate appears.
Fix:
State the desired condition in words first, then verify that the comparison matches it.Assuming the tracking variable changes on every iteration
Tracking variables update only when the loop's condition is met.
Fix:
For each item, explicitly decide whether the condition is met before recording the new state.Inspecting the result before the scan is complete
The loop may still encounter an item that changes the tracking variable.
Fix:
Inspect the tracking variable after all relevant items have been processed.
Practice the Trace
A tracking variable stores the largest value seen so far. It begins at 4, and the remaining items are 9, 2, and 6. Record the tracking value after each item is examined, then state the final value.
Hints
- Compare each current item with the tracking value immediately before that item is examined.
- Change the tracking value only when the current item is larger.
- The final answer is the tracking value after the last item.
Tracing a maximum search
The tracking value begins at 4. The scan examines 9, 2, and 6. What value remains at the end?
Start: The tracking value is 4.
Examine 9: 9 is larger than 4, so the tracking value changes to 9.
Examine 2: 2 is not larger than 9, so the tracking value remains 9.
Examine 6: 6 is not larger than 9, so the tracking value remains 9.
The final tracking value is 9.
Pattern in Other Tasks
The same initialization-computation-inspection pattern extends beyond maximum searches. A minimum search initializes a tracking value and changes it when a smaller candidate is found. A counting task changes its tracking value when an item meets the counting condition. A summing task updates its running result for each item. A search for a particular item updates its state according to whether the desired item has been found.
- Initialize a tracking variable with a clearly defined meaning.
- Process each item systematically during the loop.
- Update the tracking variable only when the intended condition is met.
- Inspect the final tracking value after the scan is complete.
- Check the initialization and comparison carefully before trusting the result.
Key Takeaways
- A data-scanning loop has three conceptual parts: initialization, computation on each item, and inspection of the result.
- A tracking variable records the current result as the loop progresses.
- The tracking variable changes only when the loop's condition is met.
- Correct initialization and comparison operators are essential for a correct result.
- The same pattern supports maximums, minimums, counting, summing, and other scanning tasks.
Key Takeaways
- Loops scan collections systematically instead of requiring manual inspection.
- The three-part structure is initialize, compute on each item, and inspect the result.
- A tracking variable changes only when the current item satisfies the comparison.
- To predict the final state, trace the tracking variable after every iteration.
- Initialization and comparison choices determine whether the scan produces the intended answer.