A Boolean answers a yes/no question. An if statement is what finally does something with the answer — it lets a program take one path or another.
These two pages are exactly today’s lesson. Do the little code exercises — they take five minutes and make class easy. Enrolled in our Runestone course? Open it from there so your progress counts.
Still in Programs. In 2.11 you learned to produce a
True or False. Today you use one: if reads the Boolean and picks which lines
of code run next. This is called selection, and it is one of the three building
blocks of every algorithm you will ever write.
That is precisely what if / else
does. The if asks the question; the indented block under it runs only when the answer
is True; the else block runs only when it’s False. Every program that behaves
differently for different inputs has a bouncer like this inside it.
An if statement has three parts: the keyword if, a
condition (a Boolean expression), and a colon — then an
indented block that runs only when the condition is True. Add else:
and a second indented block for what happens when it’s False.
if. Lines back at the left margin run no matter
what. Getting this wrong is the second-most-common Week 3 bug, right after
= vs ==.
This is one place Python and AP Pseudocode look genuinely different, so it is worth a full
side-by-side. Python uses a colon and indentation; pseudocode uses IF…
ELSE with curly braces and the block spelled out.
num = 7 if num % 2 == 0: print("even") else: print("odd")
num ← 7 IF (num MOD 2 = 0) { DISPLAY("even") } ELSE { DISPLAY("odd") }
Hover or tap a line to light its twin. Same logic, two notations: Python leans
on the colon and indent; pseudocode leans on braces. On the exam you must read the right
column and write the left. This program prints odd — trace it and see why.
num = 7: 7 % 2 is 1, and
1 == 0 is False — so the else runs and it prints
odd. Do this on paper for every trace question; guessing is how points leak.
A classic if / else: orders of $50 or more ship free; everything else
pays a $5 fee. Change the total and watch which branch runs.
if tests to decide which branch runs.if block or the
else block. Only one branch runs per pass.Five questions, all traces. Write the variable’s value, evaluate the condition, follow the matching branch — on paper — then pick your answer.
Selection is why software can be responsive instead of a
fixed recording. A thermostat runs the heater only if the room is below target. A
game shows “Game Over” only if your health hits zero. A checkout waives
shipping only if the cart clears a threshold. Each is one bouncer, one condition,
two doors.
And each condition is a choice a person made. Why $50 and not
$40 for free shipping? Why that health value? The if is neutral; the number in it is
a business or design decision with real consequences. Reading code well means noticing those
numbers, not just the logic around them.
if is what
finally reads that True/False and acts on it.elif lets you
chain many conditions to sort a value into more than two buckets.if / else are guaranteed, often written only in
pseudocode.