Unit content
Conditional control flow
A program does not always execute the same steps. A conditional chooses which branch to execute according to a Boolean condition.
A generic form is
if condition:
first branch
else:
second branch
Conditions select paths
Suppose a program classifies a temperature:
if temperature < 0:
state = "freezing"
else:
state = "above freezing"
Only one branch executes for a particular evaluation of the condition.
Multiple cases
Several conditions can be tested in sequence:
if score >= 90:
grade = "A"
else if score >= 80:
grade = "B"
else:
grade = "other"
The order matters because the first matching branch is chosen.
Nested decisions
A branch can contain another conditional when a later decision only makes sense inside an earlier case.
Deep nesting can become difficult to follow, so complex decisions are often simplified by combining Boolean expressions or extracting meaningful functions.
Conditionals connect Boolean logic with program execution: a truth value becomes a decision about which instructions run next.