Conditional Statements and Comparisons
A correct trace connects every update to the current element and its condition.
Four Questions for Every Collection
When a loop processes a collection, four questions appear repeatedly: How many elements are there? What is their total? Which element is largest? Which element is smallest? Counting, summing, and finding extremes are common loop patterns for analyzing test scores, processing sensor data, and auditing financial records.
All four patterns use the same overall structure: initialize a variable before the loop, update it while processing each current element, and use its final value after the loop.
What do you think happens?
A loop processes six elements and increments count by 1 on every iteration. What will count equal after the loop?
Reveal answer
6
The counter records how many iterations have occurred. Because it increases once for each of the six elements, its final value is 6.
Tracing Count and Sum
A counter tracks the number of elements processed. Start count at 0, then increase it by 1 during every iteration. The current element's value does not matter for counting; only the fact that the loop ran matters.
6A sum uses a total initialized to 0. During each iteration, add the current element's value to total. Unlike counting, summing uses the actual value of every element.
values = [3, 41, 12, 74, 8, 16]
total = 0
for itervar in values:
total = total + itervar
print(total)- 1Before the loop, total is 0.
- 2After adding 3, total is 3.
- 3After adding 41, total is 44.
- 4After adding 12, total is 56.
- 5After adding 74, total is 130.
- 6After adding 8, total is 138.
- 7After adding 16, total is 154.
154Comparing the Current Value
Finding the maximum means storing the largest value seen so far. Initialize largest to None. During each iteration, replace largest only when the current element is greater than the stored value, or when largest is None.
Finding the minimum follows the same pattern in the opposite direction. Initialize smallest to None. Replace smallest only when the current element is smaller than the stored value, or when smallest is None.
74
3A Complete Iteration Trace
Processing Six Values
For the values 3, 41, 12, 74, 8, and 16, track count, total, largest, and smallest after every iteration.
- 1Before iteration 1
count is 0, total is 0, largest is None, and smallest is None.
- 2After 3
count becomes 1 and total becomes 3. Because both comparison variables are None, largest and smallest both become 3.
- 3After 41
count becomes 2 and total becomes 44. Since 41 is greater than 3, largest becomes 41. Since 41 is not less than 3, smallest remains 3.
- 4After 12
count becomes 3 and total becomes 56. The current value changes neither extreme because 12 is not greater than 41 and is not less than 3.
- 5After 74
count becomes 4 and total becomes 130. Since 74 is greater than 41, largest becomes 74. Smallest remains 3.
- 6After 8 and 16
count becomes 6 and total becomes 154. Neither remaining value replaces the current largest or smallest.
count = 6, total = 154, largest = 74, smallest = 3
Debugging with State Traces
When a counting, summing, or extreme-finding loop produces an unexpected result, trace the state of its variables through every iteration. Record the initial value before the loop, then record the value after each current element is processed. The first row that differs from the expected trace identifies where the logic went wrong.
| Stage | Current element | count | total | largest | smallest |
|---|---|---|---|---|---|
| Before loop | none | 0 | 0 | None | None |
| After iteration 1 | 3 | 1 | 3 | 3 | 3 |
| After iteration 2 | 41 | 2 | 44 | 41 | 3 |
| After iteration 3 | 12 | 3 | 56 | 41 | 3 |
| After iteration 4 | 74 | 4 | 130 | 74 | 3 |
| After iteration 6 | 16 | 6 | 154 | 74 | 3 |
A state trace makes each accumulator and comparison update visible.
Trace the four variables for the values 5, 2, and 9. Write the state before the loop and after each iteration. Identify the iteration at which largest changes and the iteration at which smallest changes.
Hints
- Start count and total at 0.
- Start largest and smallest at None.
- The first element becomes both extremes.
- Compare each later value with the stored extremes.
Mistakes in Accumulator Logic
Common mistake Using the current element's value as the count update
What it looks likeAdding itervar to count instead of increasing count by 1
Why it failsCounting tracks how many iterations occurred, not the sum of the element values.
Do this insteadInitialize count to 0 and increase it by 1 on every iteration.
Common mistake Failing to initialize the sum to 0
Why it failsThe running total needs a starting value before the first element is added.
Do this insteadSet total to 0 before the loop.
Common mistake Updating largest or smallest on every iteration
Why it failsAn extreme variable should change only when the current value passes its comparison condition.
Do this insteadUpdate largest only when the current value is greater, and update smallest only when it is less.
Common mistake Using the wrong comparison direction
What it looks likeTesting whether the current value is less than largest when searching for a maximum
Why it failsThe maximum requires the greater-than comparison, while the minimum requires the less-than comparison.
Do this insteadUse the greater-than comparison for largest and the less-than comparison for smallest.
Common mistake Checking only the final result during debugging
Why it failsA final incorrect value does not reveal which iteration introduced the error.
Do this insteadRecord accumulator and comparison variables before the loop and after every iteration.
Practice and Transfer
Design one loop that processes the values 6, 14, 2, and 11. Your loop should produce the number of elements, their total, the largest value, and the smallest value. Before deciding that your result is correct, trace all four variables after each iteration.
Hints
- Use one counter initialized to 0.
- Use one total initialized to 0.
- Use largest and smallest initialized to None.
- The current value can update an extreme only when its comparison condition is true.
The important debugging question is not only what the final answer is, but when each variable changed. A correct trace connects every update to the current element and its condition.
Pattern Summary
- 1Counting starts a counter at 0 and increases it by 1 for each processed element.
- 2Summing starts a total at 0 and adds the current element's value during every iteration.
- 3Finding a maximum starts with None and replaces the stored value only when a larger element is found.
- 4Finding a minimum starts with None and replaces the stored value only when a smaller element is found.
- 5Tracing variables before and after each iteration reveals where an incorrect result first appears.
At a glance
- Initialize a counter, accumulator, or comparison variable before the loop.
- Update count once per element and add each element to total when summing.
- Use greater-than comparisons for the maximum and less-than comparisons for the minimum.
- Trace variable states after every iteration to locate the first incorrect update.
- These four loop patterns form a foundation for processing collections of data.