Chapter 3

Decisions, Repetition, and Summaries

Make a program classify several readings, repeat the same work for each one, and keep a summary that accounts for available, skipped, and unvisited values.

The previous chapter named four readings separately. That worked while every statement ran once, but it left us repeating the same kind of name and calculation. Here we will keep the readings together and let Python visit them one at a time.

We will begin with questions that have only two answers: true or false. Those answers let a program choose a branch. A loop then applies the choice repeatedly while a total and count make the changing state visible.

The examples stay small enough to trace by hand. You will check exact boundaries, repair loops that stop too early or too late, and distinguish a missing reading from the number zero. By the end, the repeated summary will be ready to become a function in the next chapter.

After this chapter

  • Evaluate comparison and combined conditions at exact boundary values.
  • Trace an if, elif, and else chain and explain why one branch is selected.
  • Use a list and a for loop to classify each available reading and update a total and count.
  • Trace and repair a while loop by checking its starting state, condition, update, and final false check.
  • Use None, continue, or break deliberately and account for processed, skipped, and unvisited values.

Lessons

  1. 01
    Ask True-or-False Questions

    Ask comparison questions, interpret True and False, and check exact boundaries.

    3 exercises
  2. 02
    Choose with if, elif, and else

    Choose one indented branch and trace order and boundary cases.

    3 exercises
  3. 03
    Combine Conditions

    Combine comparison results into readable interval and attention conditions.

    3 exercises
  4. 04
    Keep Readings in a List

    Place readings in one ordered list and count its values without introducing list operations early.

    3 exercises
  5. 05
    Repeat with for and range

    Repeat over list values or a known count and trace the excluded range stop.

    3 exercises
  6. 06
    Build a Running Summary

    Initialize, update, and trace total and count across a sequence.

    3 exercises
  7. 07
    Repeat While a Condition Holds

    Repeat under a changing condition and repair infinite or off-by-one loops.

    3 exercises
  8. 08
    Skip or Stop Deliberately

    Handle unavailable readings and choose clearly between continue, break, and an ordinary condition.

    3 exercises

Review and practice

  1. Review

    Review branches, both loop forms, running state, boundaries, missing values, and stopping decisions.

  2. Exercises

    Trace, construct, and repair decisions, repetition, summaries, and stopping policies.

Chapter progress