Skip to content
UnderDunn Courses

Decisions and Repetition

Programming FoundationsLesson 3 of 860 minutesLab included
score = 17
if score >= 20:
result = "high"
elif score >= 10:
result = "medium"
else:
result = "low"
print(result)

Python tests from top to bottom and executes the first matching branch. Indentation defines which instructions belong to each branch.

Predict results for 9, 10, 19, and 20. Values at the edges deserve attention because < and <= disagree exactly there.

total = 0
for value in [3, 5, 7]:
total = total + value
print(value, total)

Trace a table with iteration, value, and total. The final answer matters less than explaining every transition.

A while loop repeats while its condition remains true:

remaining = 3
while remaining > 0:
print(remaining)
remaining -= 1

Removing the final line creates a loop whose controlling state never approaches termination. Stop a runaway terminal program with Ctrl+C and read the resulting KeyboardInterrupt.

Knowledge check

Question 1 of 2

Why should boundary values be tested?

You can turn control flow into testable paths and trace state across repetitions. The next lesson packages behavior into functions and moves data through files.