Concepts / Comparison operators and conditionals

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.

  • Programming

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?

  • 5

  • 7

  • 11

  • 26

Reveal answer
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.

Starttracker = 73tracker = 711tracker = 115tracker = 1111final trackercompareretain 7update to 11retain 11
How does the tracking variable change after each item is compared, and what value does it hold at each position?
Worked example

A maximum-value trace

Scan the collection 7, 3, 11, 5 while keeping the largest value found so far.

  1. 1
    Initialize

    Set the tracking variable to 7, the first value used in this generated example.

  2. 2
    Compare 3

    The current item is not larger than the tracked value, so the tracker remains 7.

  3. 3
    Compare 11

    The current item is larger than the tracked value, so the tracker changes to 11.

  4. 4
    Compare 5

    The current item is not larger than the tracked value, so the tracker remains 11.

  5. 5
    Inspect

    After every item has been examined, inspect the tracking variable.

Result

The final tracking value is 11.

The Three-Part Pattern

Initializeset tracking valueCurrent itemone item at a timeComparecondition is testedUpdate trackercondition metRetain trackercondition not metInspect resultafter the loopbegin scanexamineyesnonext itemnext itemno items remain
What happens first, what computation repeats for each item, and when is the final tracking value inspected?

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.

StageQuestion to askRole in the scan
InitializeWhat should the tracker start as?Establishes the starting state
ComputeDoes the current item satisfy the comparison condition?Updates or preserves the tracker
InspectWhat 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.

Worked example

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.

  1. 1
    Current tracker is 11

    This is the state before the next comparison.

  2. 2
    Compare 5

    The condition is not met, so the retain branch is taken and the tracker stays 11.

  3. 3
    Compare 14

    The condition is met, so the update branch is taken and the tracker becomes 14.

  4. 4
    Final state

    The tracker holds the most recent value produced by the applicable branch.

Result

The final tracking value is 14.

Key point

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 like

    A 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 fails

    The later comparisons depend on the initial tracker, so an improper starting state can lead to an incorrect result.

    Do this instead

    Choose the initialization deliberately before the loop begins.

  • Common mistake

    Using the wrong comparison condition

    What it looks like

    A scan intended to find the largest value uses a condition suited to replacing the tracker with smaller values.

    Why it fails

    The branch decision determines whether the tracker changes, so reversing the intended comparison changes the task being performed.

    Do this instead

    State 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 like

    The current tracking value is treated as the final answer while items still remain.

    Why it fails

    The remaining items may still update the tracker.

    Do this instead

    Inspect the result after every relevant item has been examined.

  • Common mistake

    Assuming the tracker changes on every iteration

    What it looks like

    A learner expects the tracker to change when the current item does not satisfy the condition.

    Why it fails

    Tracking variables update only when the condition is met.

    Do this instead

    Record both outcomes: update when the condition is met and retain the existing state otherwise.

Best practice

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

EASY

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.
Worked example

Practice answer

Trace the tracker beginning at 4 through 4, 9, 2, 9, 6, updating only for a larger item.

  1. 1
    Start

    The tracker is 4.

  2. 2
    Compare 9

    9 is larger, so the tracker becomes 9.

  3. 3
    Compare 2

    2 is not larger, so the tracker remains 9.

  4. 4
    Compare 9 again

    The current item is not larger than the tracker, so the tracker remains 9.

  5. 5
    Compare 6

    6 is not larger, so the tracker remains 9.

Result

The tracking values are 4, 9, 9, 9, 9, and the final value is 9.

Key Takeaways

  1. 1
    A data-scanning loop follows the pattern initialize, compute, and inspect.
  2. 2
    A comparison condition decides whether the tracking variable updates or retains its current state.
  3. 3
    To predict the final result, trace the tracker after every item rather than looking only at the last item.
  4. 4
    Proper initialization and the correct comparison are both essential to obtaining the intended answer.
  5. 5
    The 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.

Related concepts

More concepts starting with C