Concepts / Basic for loop syntax

Basic for loop syntax

A basic data-scanning loop follows three roles: initialize a tracking variable, compute with each item, and inspect the result after the loop.

  • Programming

Why Scan Data

When a collection contains numbers, test scores, temperatures, or file contents, you may need to find something specific. You might look for the largest value, the smallest value, a count, or a total. Checking every item manually is tedious and error-prone. A loop examines the items systematically while a tracking variable keeps the useful result found so far.

Key point

A basic data-scanning loop follows three roles: initialize a tracking variable, compute with each item, and inspect the result after the loop.

The Three-Part Pattern

The syntax of a loop designed to scan data can be understood through its job sequence. First, initialization gives the tracking variable a starting state. Next, the repeated computation examines each item in the collection and updates the tracking variable only when the chosen condition is met. Finally, inspection uses the tracking variable after the loop has processed the collection.

Initializationstarting tracking stateCollectionitems to scanComputationrepeat for each itemInspectionfinal tracking statethen scaneach itemafter all items
What happens first, what computation repeats for each item, and when is the result inspected?
Best practice

Before reading or writing a loop, name these three parts: What is the initial tracking state? What computation is repeated for each item? What result will be inspected at the end?

Tracking a Maximum

Worked example

Finding the largest value

Scan the collection [7, 3, 9, 5] and track the largest value found so far.

  1. 1
    Initialize

    Start the tracking variable with the first value, 7.

  2. 2
    Process 3

    Compare 3 with the tracked value 7. The tracking variable does not change because 3 is not larger.

  3. 3
    Process 9

    Compare 9 with the tracked value 7. The tracking variable changes to 9 because 9 is larger.

  4. 4
    Process 5

    Compare 5 with the tracked value 9. The tracking variable does not change because 5 is not larger.

  5. 5
    Inspect

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

Result

The final tracking value is 9.

7initial tracking value3tracking remains 79tracking becomes 95tracking remains 99final tracking valueprocess nextprocess nextprocess nextinspect
How does the tracking variable change as the loop moves from one item to the next?

What do you think happens?

After scanning [7, 3, 9, 5] while tracking the largest value, what does the tracking variable contain?

  • 5

  • 7

  • 9

  • The number of items

Reveal answer
Answer:

9

The tracking value starts at 7, stays 7 after 3, changes to 9 when 9 is processed, and stays 9 after 5.

State Before and After

The tracking variable represents the best result found so far, not necessarily the final answer from the beginning. It can remain unchanged for several items or update when an item satisfies the comparison condition. The important state change is the transition from the initial value to the value produced after all items have been processed.

7initial value9final valuescan all items
What value does the tracking variable contain after every item has been processed?

Applying the Pattern

The same three-part structure supports many scanning tasks. For a maximum, the tracking variable changes when an item is larger than the current tracked value. For a minimum, it changes when an item is smaller. For counting, it changes when an item meets the counting condition. For summing, it changes as each item contributes to the running total. The specific computation changes, but the roles remain initialization, repeated computation, and inspection.

TaskTracking purposeUpdate idea
Finding a maximumLargest value found so farUpdate when the current item is larger
Finding a minimumSmallest value found so farUpdate when the current item is smaller
CountingNumber of qualifying items so farUpdate when the current item meets the condition
SummingRunning totalAdd each item to the tracked total

Different data-scanning tasks use the same initialization-computation-inspection pattern.

Initializetracking variableCollectionitems to examineComputeupdate when condition ismetInspectuse final resultpreparefor each itemwhen scanning ends
How are initialization, the collection, and repeated computation connected in the loop?

Common Loop Mistakes

  • Common mistake

    Choosing an unsuitable initial tracking value

    What it looks like

    Starting a maximum search with a value that can incorrectly remain larger than every item.

    Why it fails

    The tracking variable begins with the wrong state, so later comparisons may never produce the intended result.

    Do this instead

    Choose initialization deliberately and check how it relates to the collection and the task.

  • Common mistake

    Using the wrong comparison direction

    What it looks like

    Using a smaller-value comparison when the task is to find the largest value.

    Why it fails

    The tracking variable updates for the wrong items and represents a different result from the one requested.

    Do this instead

    State the task in words first, then verify that the comparison matches it.

  • Common mistake

    Expecting the tracking variable to change on every iteration

    What it looks like

    Assuming the tracked maximum must change when the next item is 3 and the current maximum is 7.

    Why it fails

    Tracking variables update only when the condition is met.

    Do this instead

    For each item, ask whether the condition is true before changing the tracking state.

  • Common mistake

    Inspecting the result before the collection is fully scanned

    What it looks like

    Treating the current tracking value as the final maximum while unprocessed items remain.

    Why it fails

    A later item may still satisfy the condition and change the result.

    Do this instead

    Inspect the tracking variable after every item has been processed.

Practice the State Trace

EASY

A loop scans the collection [4, 11, 6, 13] while tracking the largest value. Describe the tracking value after initialization, after each item is processed, and after the final inspection.

Hints
  • Begin with the first value as the initial tracking state.
  • For each later item, compare it with the current tracked value.
  • Write down the tracking value even when it does not change.
MEDIUM

Describe how the same three-part pattern would change if the task were finding the smallest value instead of the largest value. Identify the initialization role, the repeated computation, and the final inspection.

Hints
  • Keep the collection-scanning role unchanged.
  • Change the comparison used to decide when the tracking variable updates.
  • The final inspection still occurs after every item has been processed.

Key Takeaways

  1. 1
    A data-scanning loop initializes a tracking variable, computes with each item, and inspects the result at the end.
  2. 2
    The tracking variable represents the result found so far and updates only when the condition is met.
  3. 3
    The same pattern supports maximums, minimums, counting, summing, and other scanning tasks.
  4. 4
    Correct initialization and comparison operators are essential to obtaining the intended result.
  5. 5
    To predict the final state, trace the tracking variable one item at a time.

At a glance

  • A basic scanning loop has three roles: initialization, repeated computation, and final inspection.
  • A tracking variable changes state only when the current item satisfies the chosen condition.
  • Maximums, minimums, counts, and sums use variations of the same pattern.
  • Tracing the tracking value after each item makes the final state predictable.
  • Initialization and comparison choices determine whether the loop produces the intended answer.

Related concepts

More concepts starting with B