cadence_learn
think_like_a_programmer / level 10 of 12 ▶ 13 min

true, false, nothing else.

Every decision a program makes collapses to one bit first. Flip switches through the truth tables, then watch one wrong joining word put a three-year-old on a rollercoaster.

after this level you'll be able to
  • Fill in the truth tables for AND, OR and NOT from memory
  • Decide whether a real rule needs AND or OR, and say why
  • Spot the boundary case in any comparison you write
the whole idea

everything a program decides comes down to one bit

the one sentence

Before a program can choose anything, the question it is asking has to collapse to a single value: true or false. Nothing in between, no maybe, no “sort of”.

Level 3 gave you the if shape. This level is about the question inside it. Real conditions are rarely one thing — they are several facts joined together, and there are exactly three ways to join them.

AND means both must hold. OR means at least one must. NOT flips it. That is the complete set. This part of the subject is called Boolean logic, after George Boole, who worked it out in the 1850s — about ninety years before anyone had a computer to run it on.

the complete rulebook, in three tables
AND — BOTH MUST BE TRUE ABRESULT falsefalsefalse truefalsefalse falsetruefalse truetruetrue one row out of four. AND is strict — it is usually false. OR — EITHER WILL DO ABRESULT falsefalsefalse truefalsetrue falsetruetrue truetruetrue three rows out of four. OR is generous — it is usually true. NOT — FLIP IT ARESULT falsetrue truefalse the only one with two rows, because it takes one input. A condition in a program is always one of these, however complicated it looks. Everything reduces to true or false before anything decides.
Four rows is the whole of AND. If you can fill these tables in from memory you can read any condition in any language — and you can prove your own logic right before you run it.
try it yourself

flip the switches

Two facts, one joining word. Change either switch and watch which row of the table you land on.

▤ lab 10a · the logic bench

The highlighted row is the one you are standing on right now.

A
B
false AND false
false
ABresult
Flip a switch. With AND you have to get all the way to the bottom row before anything is true.
◈ ask an ai about this

“What is Boolean logic? Explain AND, OR and NOT with an everyday example and show me the truth tables.”

chatgpt ↗ claude ↗

where it bites

one wrong joining word lets the wrong people in

This is not an abstract exercise. Choosing AND where you meant OR is a real bug that ships, and it never looks wrong when you read it back. Here is a rule you have met in real life — a fairground ride.

▤ lab 10b · who gets on the ride

The rule: tall enough and old enough. Now switch the joining word and watch what happens.

112 cm
11 yrs
not allowed on
Drag the sliders. Then press the OR version and drag them to the same place.
read it out loud, in English

With OR selected, a three-year-old who happens to be tall gets on the ride, and so does a sixteen-year-old who is short. The code is not broken, the logic compiles, every test of “does it run” passes — and the rule is wrong. This is why your course description says “correct and maintainable” and not just “working”.

◈ ask an ai about this

“How do I decide whether a rule needs AND or OR? Give me an example where choosing wrong lets the wrong person through.”

chatgpt ↗ claude ↗

one more trap

the boundary is where the bugs live

Look closely at height ≥ 120. Is a child of exactly 120cm allowed on? With , yes. With >, no. One character, and one specific child is turned away.

Every condition you write has an edge like this, and it is almost never the middle of the range that goes wrong. It is the exact boundary. When you test your own work — the next level — the boundary is the first value you should try, not the last.

a habit worth stealing

Whenever you write a comparison, immediately say out loud what happens at the exact number. “At 120 they get on.” If you cannot answer that instantly, you do not yet know what you wrote.

do this together

argue about the rules

☶ two people · 10 minutes

One writes a rule in English. The other finds the person it treats unfairly.

person a — the rule-maker

Write a real rule with two conditions. “Free delivery if the order is over £50 and you are a member.” “You can borrow the car if you passed your test and it is not raining.”

person b — the awkward customer

Find someone the rule handles badly. Someone at exactly £50. Someone who meets one half. Then ask: should that be AND, or OR?

You will find that most real rules are argued about at the boundary, not in the middle — and that plain English is genuinely ambiguous about it. “Over £50” and “£50 or more” are different rules, and a program has to pick one. Deciding which is your job, not the compiler’s.

what to keep

three things worth remembering

01

Three joining words, no more

AND, OR, NOT. Every condition you will ever read is built from those.

02

AND is strict, OR is generous

AND is true in one row of four. OR is true in three. Picking the wrong one is a real, shippable bug.

03

Check the exact boundary

What happens at precisely 120? If you cannot say instantly, you do not know what you wrote.

check yourself

4 questions before you move on

Not recall — these are the shapes an exam actually uses. Every answer below was produced by compiling and running the code, so if you disagree with one, the compiler is the one to believe.

▢ check yourself4 questions

Have a real go before revealing. Being wrong here is worth more than being right in three weeks.

01

A and B are joined by AND. Out of the four possible combinations, in how many is the result true?

02

And with OR?

03

A ride needs height ≥ 120 and age ≥ 8. Someone changes it to or. Who gets on that should not?

04

A rule says height ≥ 120. What happens to someone who is exactly 120?

answered: 0 of 4right first time: 0
Stuck? Peter reads these personally and replies to your email.
Ask Peter →