What you will learn
  • Explain controlled repetition in plain language.
  • Trace information from input to output.
  • Test one subsystem independently.
  • Recognize a common failure and respond safely.
  • Modify the example without changing several variables at once.

The central idea: controlled repetition

A loop repeats a block of code. A for loop is suited to a known collection or count; a while loop repeats while a condition remains true and therefore needs a reliable way to stop.

Treat the system as a chain of observable inputs, explicit processing, a decision, and an output. That makes the design easier to explain and gives you a place to test when something fails. A working result is not enough: record what input produced it and what evidence shows the result is correct.

Follow the information through the system

Read the diagram from left to right. Each arrow is a boundary where data, electrical signals, or commands move between parts. Test those boundaries separately before combining the whole system.

A program examines five readings, prints each one, and counts how many exceed a warning threshold.

  • Prefer a for loop when the items are known.
  • Define the stopping condition before a while loop.
  • Keep a maximum-attempt limit around external input.
Start → get item → process → more items? ─Yes↺
                                      └No→ finish

A small working example

Run this example exactly as shown before modifying it. Predict the output first, compare that prediction with the real result, and change only one part at a time.

Python
readings = [18, 24, 31, 27, 40]
warning_count = 0
for reading in readings:
    if reading > 30:
        warning_count += 1
print(warning_count)

The accumulator increases only when the current reading crosses the threshold.

Expected result: 2

Test before you trust the result

Start with a normal case, then test a boundary case and a failure case. For software, try empty, unexpected, and extreme inputs. For hardware, begin with wheels raised or actuators disconnected, keep a physical power switch within reach, and verify safe stopping behavior.

Write down the expected result before each test. If the observation differs, stop and find the first boundary where the expected signal disappeared. This method is slower than changing many things at once, but it produces evidence instead of guesses.

  • Test one input with a known value.
  • Observe the intermediate value before the final action.
  • Confirm a safe response when input is missing or invalid.
  • Repeat the same test to check consistency.

Important terms

Iteration
One pass through a loop.
Iterable
An object that supplies items one at a time.
Loop variable
The name holding the current item.
Infinite loop
A loop whose stopping condition is never reached.
Accumulator
A variable updated across iterations.

Mini project: practice controlled repetition

  1. Create a list of five readings.
  2. Loop through and print them.
  3. Count values above a threshold.
  4. Check the count by hand.
  5. Try an empty list.

Common mistakes and debugging

  • Forgetting to update a while-loop condition.
  • Changing a list while iterating over it.
  • Using a loop when a direct calculation is clearer.
  • Ignoring the empty-collection case.

Independent challenge

Print the numbers 1 through 20, but replace multiples of 3 with sensor check. Explain which test should run first if you add multiples of 5.

Check your understanding: 10 questions

  1. What is the main purpose of controlled repetition?

  2. What does “Iteration” mean in this lesson?

  3. How is “Iterable” used?

  4. Why should you test one boundary or subsystem at a time?

  5. What should the system do when an input is missing or invalid?

  6. In your own words, what does “Iteration” mean?

  7. In your own words, what does “Iterable” mean?

  8. In your own words, what does “Loop variable” mean?

  9. In your own words, what does “Infinite loop” mean?

  10. In your own words, what does “Accumulator” mean?

Quiz answers

Reveal all 10 answers after your attempt
  1. A loop repeats a block of code. A for loop is suited to a known collection or count; a while loop repeats while a condition remains true and therefore needs a reliable way to stop.
  2. One pass through a loop.
  3. An object that supplies items one at a time.
  4. It reveals where the expected value or behavior first changes, so debugging is based on evidence.
  5. It should reject the input, report the problem, or move to a documented safe state instead of guessing.
  6. One pass through a loop.
  7. An object that supplies items one at a time.
  8. The name holding the current item.
  9. A loop whose stopping condition is never reached.
  10. A variable updated across iterations.

Summary

controlled repetition becomes manageable when you identify the input, processing, decision, output, and evidence of success. Build the smallest version, test it, then make one measured change.

Continue learning

Continue to the next lesson, which builds on this model with one additional capability.

Choose a connected learning path

Sources and further reading

Prepared 2026-10-02. Editorially completed for the course. Software examples are designed for current Python 3; verify board, operating-system, library, and hardware details against the linked primary documentation before a physical build.