Control Flow (if-else)

Lesson 4 of 20

Teach your program to choose — watch if, elif and else route execution down exactly one branch at a time.

The big picture

Teaching code to choose

Real programs react: an if checks a condition and runs one branch while skipping the rest.

if = Gatekeeper

The indented body under if runs only when the condition is True.

elif = Next Check

Tried only when every earlier check failed — the chain stops at the first match.

else = Safety Net

Runs when nothing above matched; it has no condition of its own.

Indentation Rules

Everything indented under the if belongs to it; Python uses 4 spaces by convention.