Skip or Stop Deliberately
Represent an unavailable reading with None, skip it without treating it as zero, and distinguish continue from break. Account for processed, skipped, and unvisited values.
Real measurements may contain an unavailable value. The program must account for it without treating it as a temperature.
Represent an Unavailable Reading
Use None when a value is unavailable:
readings = [18, 21, None, 20, 23]
None is not zero and is not a missing number that arithmetic can use. Test it
with is None before a numerical comparison or addition.
continue stops the current iteration and begins the next one. The unavailable
value is skipped, but later readings are still visited.
| List value | Decision | Result |
|---|---|---|
| 18 | available | processed |
| 21 | available | processed |
None | unavailable | skipped |
| 20 | available | processed |
| 23 | available | processed |
Q1. Skip an unavailable reading
What values does this loop add to total?
Select one choice, then check.
HintContinue means next iteration
It does not end the complete loop.
SolutionThe available values are added
The loop adds 3, skips None, then adds 5. The final total is 8.
Stop at an Explicit Marker
break ends the nearest enclosing loop immediately. Suppose None means that
the remaining readings should not be used rather than that one reading should
be skipped:
| List value | State after the loop |
|---|---|
| 18, 21 | processed |
None | stop marker |
| 20, 23 | unvisited |
The same data produces a different result because continue and break state
different policies. Choose between them from the meaning of the marker, not
from which keyword is shorter.
Q2. Account for unvisited values
For [18, None, 20], which result follows from using break at None?
Select one choice, then check.
HintBreak ends repetition
Values after the marker are never visited by this loop.
SolutionThe final reading is unvisited
The loop processes 18, reaches the stop marker, and ends. It never reaches 20.
Prefer an Ordinary Condition When It Is Clearer
continue can remove deeply nested code, and break can express a real early
stop. They are not required for every condition. If the loop merely needs to
add positive readings, an ordinary condition is direct:
Use continue when naming a skipped case makes the remaining body easier to
read. Use break only when later values must remain unvisited.
Q3. Choose the clearest control
Every reading should be visited, but unavailable readings should not enter the summary. Which control best states that policy?
Select one choice, then check.
HintDo not stop early
The loop must still visit readings that appear after the unavailable value.
SolutionSkip only the unavailable iteration
continue moves to the next reading. break would incorrectly leave all
later values unvisited.
None records absence rather than a numerical value. continue skips one
iteration; break leaves later values unvisited. A trustworthy summary makes
processed, skipped, and unvisited data explicit. The next chapter will give
this summary calculation a name and reuse it.