Conditionals, Making Decisions
Here is the plain idea: a conditional lets a program make a decision. It asks a yes-or-no question, and depending on the answer, it does one thing or another. In plain words: "If it's raining, take an umbrella; otherwise, don't."
That little word if is the heart of nearly every decision a program makes.
The fork-in-the-road analogy
Picture walking a path that splits in two. At the fork stands a sign with a question: "Is it raining?" Yes — take the left path toward the umbrella stand. No — take the right path and head straight out.
A conditional is that fork. The program reaches the question, checks the answer, and walks down exactly one of the two paths. It can't take both. The yes-or-no answer decides which way it goes.
How it reads
In nearly every language, the shape is the same and reads almost like English:
- if (some question is true) → do this block of steps
- otherwise → do that block of steps instead
The question always boils down to true or false — exactly the boolean type from the last lesson. "Is age greater than 18?" is either true or false, and that single answer steers the whole program.
The "otherwise" part is optional. Sometimes you only act when the answer is yes, and do nothing at all when it's no. "If the cart is empty, show a message" — and if it isn't empty, just carry on.
Stacking more paths
Forks can have more than two prongs. You can chain questions: "If the score is above 90, grade A; otherwise if it's above 80, grade B; otherwise, grade C." The program tries each question top to bottom and takes the first path whose answer is yes.
This is how programs handle the messy, many-sided real world — not one big choice, but a tidy ladder of smaller yes-or-no questions, checked in order.
Comparisons make the questions
The questions usually come from comparing values: is this equal to that? Greater? Less? age > 18, password == "secret", cart_count == 0. Each comparison produces a true or false, and that's what the conditional acts on.
A small caution that trips up everyone: checking whether two things are equal is usually written with a double equals (==), to tell it apart from putting a value in a box (a single =). One asks a question; the other fills a jar.
Your turn
No computer needed — just play the part of the signpost. Imagine a movie ticket booth with one rule ladder: under 13 pays the child price, 65 or older pays the senior price, and everyone else pays the adult price. Read it as a chain of forks: "If age is under 13 → child; otherwise if age is 65 or over → senior; otherwise → adult."
Now walk three customers down the ladder and say which price each one pays:
- a 10-year-old
- a 40-year-old
- a 70-year-old
(They land on child, adult, and senior. Notice the program checks each question top to bottom and stops at the first yes.)
So: a conditional is a fork in the road, the signpost is a yes-or-no question, and the answer decides which path the program walks. Next, we'll teach a program to repeat itself. 🔦
Stuck or curious?
Ask Pip about this lesson — tap the porthole bottom-right.