Concepts / Basic for loop syntax

Basic for loop syntax

Loops scan through data by following a three-part structure: initialize a tracking variable, compute on each item, and inspect the result.

  • 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.

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.

then scaneach itemafter all itemsInitializationstarting tracking stateCollectionitems to scanComputationrepeat for each itemInspectionfinal tracking state
What happens first, what computation repeats for each item, and when is the result inspected?

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

Finding the largest value

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

Initialize: Start the tracking variable with the first value, 7.

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

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

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

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

The final tracking value is 9.

process nextprocess nextprocess nextinspect7initial tracking value3tracking remains 79tracking becomes 95tracking remains 99final tracking value
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.

scan all items7initial value9final value
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.

preparefor each itemwhen scanning endsInitializetracking variableCollectionitems to examineComputeupdate when condition ismetInspectuse final result
How are initialization, the collection, and repeated computation connected in the loop?

Common Loop Mistakes

  • Choosing an unsuitable initial tracking value

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

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

  • Using the wrong comparison direction

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

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

  • Expecting the tracking variable to change on every iteration

    Tracking variables update only when the condition is met.

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

  • Inspecting the result before the collection is fully scanned

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

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

Key Takeaways

  • 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.