Comparison operators and conditionals
To predict the final state, do not look only at the collection's last item. Follow the tracker after every comparison. A later item can update the state, or it can leave the existing state unchanged.
Why Scan Data
When a collection contains numbers, scores, temperatures, or file contents, you may need to find something specific: the largest value, the smallest value, a count, a total, or whether a particular item exists. Checking every item manually is tedious and error-prone. A loop gives you a systematic way to examine the collection while a tracking variable records what has been found so far.
The central idea is to separate the task into three parts: initialize a tracking variable, compute an update for each item, and inspect the result after the loop. A comparison determines whether the current item should change the tracking variable. If the condition is met, the state changes; otherwise, the current state remains available for the next item.
Tracking the Largest Value
What do you think happens?
A tracking variable begins with 7 and scans the collection 7, 3, 11, 5. It changes only when the current item is larger than the tracked value. What value remains at the end?
Reveal answer
11
The tracker begins at 7. The value 3 does not replace it, 11 does replace it, and 5 does not replace 11. The final tracked value is therefore 11.
A maximum-value trace
Scan the collection 7, 3, 11, 5 while keeping the largest value found so far.
- 1Initialize
Set the tracking variable to 7, the first value used in this generated example.
- 2Compare 3
The current item is not larger than the tracked value, so the tracker remains 7.
- 3Compare 11
The current item is larger than the tracked value, so the tracker changes to 11.
- 4Compare 5
The current item is not larger than the tracked value, so the tracker remains 11.
- 5Inspect
After every item has been examined, inspect the tracking variable.
The final tracking value is 11.
The Three-Part Pattern
Initialization gives the tracking variable a starting state. Computation happens during the loop: each item is compared with the current tracked value, and the tracker changes only when the condition requires an update. Inspection happens after the scan, when the final state is used as the answer.
| Stage | Question to ask | Role in the scan |
|---|---|---|
| Initialize | What should the tracker start as? | Establishes the starting state |
| Compute | Does the current item satisfy the comparison condition? | Updates or preserves the tracker |
| Inspect | What value remains after all items are examined? | Uses the final tracking state |
The three stages of a data-scanning loop
Branch Decisions
A conditional branch connects the comparison to the tracking variable's next state. When the comparison condition is met, the loop takes the update branch. When the condition is not met, it takes the retain branch. This is why the tracker does not necessarily change on every iteration: its state changes only when the comparison calls for it.
Following both branches
A tracker currently holds 11. The next items to examine are 5 and then 14. The update condition is whether the current item is larger than the tracker.
- 1Current tracker is 11
This is the state before the next comparison.
- 2Compare 5
The condition is not met, so the retain branch is taken and the tracker stays 11.
- 3Compare 14
The condition is met, so the update branch is taken and the tracker becomes 14.
- 4Final state
The tracker holds the most recent value produced by the applicable branch.
The final tracking value is 14.
To predict the final state, do not look only at the collection's last item. Follow the tracker after every comparison. A later item can update the state, or it can leave the existing state unchanged.
Mistakes That Change Results
Common mistake Starting the tracking variable with an unsuitable value
What it looks likeA scan intended to find a largest value begins from a value that does not provide a sound starting state for the data being examined.
Why it failsThe later comparisons depend on the initial tracker, so an improper starting state can lead to an incorrect result.
Do this insteadChoose the initialization deliberately before the loop begins.
Common mistake Using the wrong comparison condition
What it looks likeA scan intended to find the largest value uses a condition suited to replacing the tracker with smaller values.
Why it failsThe branch decision determines whether the tracker changes, so reversing the intended comparison changes the task being performed.
Do this insteadState the desired update rule in words first, then verify that the comparison matches it.
Common mistake Inspecting the tracker before the scan is complete
What it looks likeThe current tracking value is treated as the final answer while items still remain.
Why it failsThe remaining items may still update the tracker.
Do this insteadInspect the result after every relevant item has been examined.
Common mistake Assuming the tracker changes on every iteration
What it looks likeA learner expects the tracker to change when the current item does not satisfy the condition.
Why it failsTracking variables update only when the condition is met.
Do this insteadRecord both outcomes: update when the condition is met and retain the existing state otherwise.
When debugging a scanning loop, make a small trace table with one row for each item. Record the current item, the comparison outcome, and the tracking value after the branch. This makes initialization errors and incorrect comparison choices visible.
Practice the Trace
A tracking variable begins at 4 and scans the collection 4, 9, 2, 9, 6. It updates only when the current item is larger than the tracked value. Write the tracking value after each comparison, then state the final value.
Hints
- Begin with the initialized value 4.
- For each item, decide whether the update condition is met.
- If it is not met, carry the previous tracking value forward.
Practice answer
Trace the tracker beginning at 4 through 4, 9, 2, 9, 6, updating only for a larger item.
- 1Start
The tracker is 4.
- 2Compare 9
9 is larger, so the tracker becomes 9.
- 3Compare 2
2 is not larger, so the tracker remains 9.
- 4Compare 9 again
The current item is not larger than the tracker, so the tracker remains 9.
- 5Compare 6
6 is not larger, so the tracker remains 9.
The tracking values are 4, 9, 9, 9, 9, and the final value is 9.
Key Takeaways
- 1A data-scanning loop follows the pattern initialize, compute, and inspect.
- 2A comparison condition decides whether the tracking variable updates or retains its current state.
- 3To predict the final result, trace the tracker after every item rather than looking only at the last item.
- 4Proper initialization and the correct comparison are both essential to obtaining the intended answer.
- 5The same structure supports finding maximums, minimums, counting, summing, and other scanning tasks.
At a glance
- Loops scan collections by initializing a tracking variable, computing an update for each item, and inspecting the result.
- Comparison conditions determine whether the tracker changes or stays the same.
- A reliable trace records the tracker after every item.
- Initialization and comparison choice must both match the task.
- The pattern applies to maximums, minimums, counts, sums, and other data-scanning problems.