Unit 2 · Programming 1 · Lesson 2.12

Selection: if / else

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.

Big Idea 3 — Algorithms & ProgrammingSelection · the core of every algorithm

Before this lesson — read on Runestone

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.

1

Where We Are

People→ Bits→ Programs→ Internet→ Security→ Big Data→ Impact

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.

2

The Hook

A bouncer with a rule list. “On the list? Come in. Not on the list? Turn around.” The bouncer checks one condition and sends you down one of exactly two paths. There is no third door.

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.

3

The Idea

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.

Indentation is not decoration in Python — it is the grammar. The indented lines are “inside” the 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.

Python

num = 7
if num % 2 == 0:
    print("even")
else:
    print("odd")

AP Pseudocode

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.

Trace before you trust. A trace table is how you predict what a branch does: write the variable’s value, evaluate the condition to True or False, then follow only the matching block. For 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.
4

Try It — The Free-Shipping Checker

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.

cart total = $
Presets:
condition (total >= 50): —
branch taken: —
output: —
5

Vocabulary

selection Programs
A program choosing which lines of code to run based on a condition. One of the three core building blocks, with sequencing and iteration.
condition Programs
The Boolean expression an if tests to decide which branch runs.
branch Programs
One of the paths a program can take — the if block or the else block. Only one branch runs per pass.
6

Check

Five questions, all traces. Write the variable’s value, evaluate the condition, follow the matching branch — on paper — then pick your answer.

0 of 5 answered
7

Impact Check

The two-path idea, everywhere at once

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.

8

Connections

Came from 2.11 — you learned to build a Boolean. The if is what finally reads that True/False and acts on it.
Next in 2.13 — two doors isn’t always enough. elif lets you chain many conditions to sort a value into more than two buckets.
Returns in 2.15 — Decision Day, where you build a full ticket-price calculator out of nothing but conditions.
Returns on the exam — “what does this program display?” trace questions over if / else are guaranteed, often written only in pseudocode.
← 2.11 Booleans and Comparisons