Concepts / Variable Assignment and Operators

Variable Assignment and Operators

Counting uses a counter initialized to 0, incremented by 1 on each iteration, to track the number of elements processed.

  • Programming

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 tasks in data processing. Each pattern uses a variable initialized before the loop, updated while the loop runs, and read after the loop completes.

The important skill is not only writing the update expression. It is also tracing the variable's state after every iteration so that you can see how the final result was produced.

A Loop State Trace

A counter records how many elements have been processed. It starts at 0 because no elements have been processed before the loop begins. Each iteration increases it by 1. After six iterations, the counter is 6. The value of the current element is not needed for this task; only the fact that the loop ran once for each element matters.

process one elementprocess one elementprocess one elementStartcount 0; total 0; max None;min NoneElement 3count 1; total 3; max 3;min 3Element 41count 2; total 44; max 41;min 3Element 12count 3; total 56; max 41;min 3
How do the counter, sum, maximum, and minimum variables change after each element is processed?

Counting and Summing

Counting and summing have similar structures but update their variables differently. A counter increases by 1 for every element. A total increases by the current element's value. Thus, counting measures the number of loop iterations, while summing accumulates the data values encountered during those iterations.

python

For the count variable, the current value of itervar is irrelevant. For total, the current value is essential because that value is added to the running result. In the source example, the total progresses through 0 + 3 = 3, 3 + 41 = 44, and 44 + 12 = 56 before continuing through the remaining elements. The final total is 154 for all six numbers.

add 3add 41add 12continue adding remaining valuestotal 0before processingtotal 30 + 3total 443 + 41total 5644 + 12total 154after six elements
What value does the sum accumulator hold before and after each iteration as elements are added?

Comparing Extremes

Maximum and minimum searches use a comparison variable. The variable begins as None, meaning that no element has been selected yet. On the first element, the comparison with None allows that element to become the current maximum or minimum. Later elements replace the stored value only when they satisfy the relevant comparison.

python

The greater-than operator controls the maximum search. When the current element is greater than the stored largest value, largest is updated. The less-than operator controls the minimum search. When the current element is less than the stored smallest value, smallest is updated.

greater-than comparison succeedsboth comparisons failgreater-than comparison succeeds3max 3; min 34141 > 3; 41 < 3 is false1212 > 41 is false; 12 < 3 isfalse7474 > 41; maximum becomes 74
When does the current maximum or minimum change, and which comparison causes the update?

In the source trace, largest becomes 3 on the first iteration, changes to 41 when 41 is compared with 3, remains 41 when 12 is processed, and later changes to 74 because 74 is greater than 41. The final maximum is 74. For the minimum search, smallest becomes 3 on the first iteration and never changes because no later value is less than 3. The final minimum is 3.

Tracing a Complete Update

Tracing count and total

Trace count and total while processing the values 3, 41, and 12.

Before the loop: Set count to 0 and total to 0 because no elements have been processed yet.

After 3: Increase count by 1, giving count 1. Add 3 to total, giving total 3.

After 41: Increase count by 1, giving count 2. Add 41 to total, giving total 44.

After 12: Increase count by 1, giving count 3. Add 12 to total, giving total 56.

After these three iterations, count is 3 and total is 56.

What do you think happens?

If the current largest value is 41 and the next element is 12, does largest change?

  • Yes, it changes to 12
  • No, it remains 41
Reveal answer

Answer: No, it remains 41.

The maximum is updated only when the current element is greater than the stored largest value. Since 12 is not greater than 41, the stored value does not change.

count = 0 total = 0 for itervar in values: count = count + 1 total = total + itervar

Debugging by State

When a result is unexpected, write down the variable's value before the loop and after every iteration. For counting, record the counter. For summing, record the total before and after each addition. For maximum and minimum searches, record the current element, the comparison result, and whether the stored extreme changed.

A small update mistake can compound across iterations. The source describes an error in which an extra value is added inside the loop. Tracing total after each step identifies the exact iteration where the result first becomes different from the expected running total.

TaskInitial valueUpdate during each iterationFinal meaning
Count0Increase by 1Number of processed elements
Sum0Add the current elementSum of all elements
MaximumNoneReplace when the current element is greaterLargest element found
MinimumNoneReplace when the current element is smallerSmallest element found

The four loop-processing patterns

Mistakes in Loop Updates

  • Starting a counter or total with a value other than 0.

    The variable already claims that one element has been processed before the loop begins.

    Fix: Initialize the counter to 0. Initialize the sum total to 0.

  • Adding the current element to the counter instead of increasing the counter by 1.

    Counting tracks how many elements were processed, not the values of those elements.

    Fix: Increase the counter by 1 on every iteration.

  • Updating the maximum when the current value is smaller.

    A maximum must be replaced only by a larger value.

    Fix: Use the greater-than comparison for the maximum.

  • Updating the minimum when the current value is larger.

    A minimum must be replaced only by a smaller value.

    Fix: Use the less-than comparison for the minimum.

  • Skipping the state trace when the final result looks wrong.

    The final value does not show which iteration caused the logic to diverge.

    Fix: Record the accumulator or comparison variable before the loop and after each iteration.

Practice the Four Patterns

MEDIUM

For one collection of values, write a loop that produces a count, a total, a maximum, and a minimum. Before running it, write the initial value of each variable. Then create a row for every element and record the counter, total, maximum, and minimum after that element is processed.

Hints
  • Initialize count and total to 0.
  • Initialize largest and smallest to None.
  • Increase count by 1 on every iteration.
  • Add the current element to total.
  • Update largest only when the current element is greater.
  • Update smallest only when the current element is smaller.

The Reusable Pattern

  1. Counting starts a counter at 0 and increases it by 1 for each processed element.
  2. Summing starts a total at 0 and adds each element's value to it.
  3. Maximum searches start with None and replace the stored value when a greater element is found.
  4. Minimum searches start with None and replace the stored value when a smaller element is found.
  5. Tracing each variable before and after every iteration reveals where an unexpected result begins.

Key Takeaways

  • Initialize a loop variable before processing the collection.
  • Update the variable inside the loop according to the task: count, add, compare for greater, or compare for smaller.
  • Use 0 for counting and summing, and None before the first candidate is selected for maximum and minimum searches.
  • Trace variable state after every iteration to debug unexpected results.