Concepts / Finding the Maximum Value

Finding the Maximum Value

For maximum-finding, the tracking variable represents the largest value found so far.

  • Programming
Interactive lab

Try it: Loop Tracer

How a Python for loop visits each item in turn, and how an accumulator variable (a total, a count, a best-so-far or a result list) changes on every iteration.

How it works

  1. Initialise the accumulator before the loop.
  2. Each iteration, the loop variable takes the next value from the list.
  3. An if inside the loop decides whether to update the accumulator.
  4. After the last item the loop ends and the accumulator holds the answer.

Default run (13 steps): values = [4, 9, 2, 7, 5]. Run the loop one statement at a time. … The loop has used every value. print(total) shows 27.

Simplified: A model of five fixed loop programs (sum, count, maximum, minimum, filter) run on your list — it steps the program exactly as Python would, but it does not run Python.

Educational simulation

Loading the simulation…

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.

Key point

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

InitializeSet a starting resultScan each itemCompute during iterationExamine resultRead after the loopbefore looploop completes
What happens before the loop, during each iteration, and after the loop completes?

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.

3initial max_value7new maximum7after examining 29new maximum9after examining 5examine 7examine 2examine 9examine 5
How does the current maximum change, or stay the same, as each data item is examined?
Item examinedCurrent maximum after the comparisonState change
Starting value3Initialized
77Changed because 7 is larger than 3
27Stayed the same because 2 is not larger than 7
99Changed because 9 is larger than 7
59Stayed 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

max_valuecurrent maximumcurrent itemvalue being examinedNew maximumreplace tracked valueCurrent maximumkeep tracked valueincoming item is largercompareincoming item is not largercompare
How does each incoming value connect to the current maximum, and what determines whether the maximum is replaced?

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.

Key point

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

3starting max_value9final max_valueupdates during scanning
What value does the maximum variable start with, and how does that starting choice relate to the final result?

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.

  • Common mistake

    Initializing the tracking variable inside the loop

    What it looks like

    The maximum-tracking state is reset while items are still being scanned.

    Why it fails

    The variable needs a starting value before the loop and must retain updates from earlier iterations.

    Do this instead

    Initialize the result-tracking variable before scanning begins.

  • Common mistake

    Updating the maximum for every item

    What it looks like

    A smaller incoming value replaces the current maximum.

    Why it fails

    State should change only when the comparison condition is met.

    Do this instead

    Replace the current maximum only when the current item is larger.

  • Common mistake

    Examining the result before the loop finishes

    What it looks like

    The tracked value is treated as final while some items remain unexamined.

    Why it fails

    The final result is available only after every item has been processed.

    Do this instead

    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 taskWhat changesWhat remains constant
Find a maximumTrack and conditionally replace the largest valueInitialize, process each item, examine after the loop
Find a minimumTrack and conditionally replace the smallest valueInitialize, process each item, examine after the loop
Sum valuesUpdate a running total for each itemInitialize, process each item, examine after the loop
Count itemsUpdate a count when the relevant condition is metInitialize, process each item, examine after the loop

Different scanning tasks vary in their initialization and update work, but share the same three-stage construction.

MEDIUM

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

  1. 1
    Initialize result-tracking variables before the loop so they have a starting value.
  2. 2
    Process each item inside the loop and use a comparison or test to decide whether the state changes.
  3. 3
    The current maximum changes only when the incoming item is larger; otherwise it remains unchanged.
  4. 4
    Examine the final tracking value after the loop has completed.
  5. 5
    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.

Related concepts

More concepts starting with F