Skip to content
All posts

True or false? How computers decide with "and", "or" and "not"

Two simple circuits side by side, each with a battery, a bulb and two paper switches in a row. On the left both switches are on, the bulb shines and a check mark sits beside it; on the right one switch is off, the bulb is dark and a cross sits beside it.

A game asks "Do you have the key and enough coins?" before letting a character open a door. A tablet only unlocks when the code is right. A night light comes on when it is dark and someone moves. Behind each of these choices, a computer is asking very simple questions with only two possible answers: true or false.

The three small words "and", "or" and "not" are how a program joins these questions together. Children aged about 8 to 12 can learn them through play, long before they write any code. This article gives you a hands-up game, two paper switches and a rule to write for an automatic door.

Every question has two answers

For a computer, a question such as "Is it raining?" or "Is the score above 10?" has only two possible answers: true or false. Programmers call this kind of question a condition.

Start with a quick game. Say a sentence, and your child shows a thumbs-up if it is true and a thumbs-down if it is false:

  • "We are in the kitchen."
  • "The cat has four legs."
  • "Today is Sunday."
  • "There are more than five spoons in this drawer." (Your child has to check!)

Some sentences can't be judged, like "Blue is the best color." That is a good thing to notice: a computer can only use a condition when it can be checked.

The hands-up game: "and" or "or"?

Gather the family and read out a rule. Everyone who fits the rule raises a hand.

  • "Hands up if you wear glasses and you have a blue top on." You need both to be true.
  • "Hands up if you have a cat or a dog." One is enough. Someone with both a cat and a dog raises a hand too.

That last point surprises many children. In everyday speech, "Do you want juice or milk?" usually means you pick one. For a computer, "or" means "at least one of them". Ask your child to explain the difference in their own words.

Try this puzzle on paper before playing it for real. Here are four family members:

Glasses Blue top Has a cat Has a dog
Mom yes no yes no
Dad no yes no no
Grandma yes yes no yes
Your child no no yes yes

Who raises a hand for each rule?

  1. Glasses and blue top
  2. Cat or dog
  3. Not wearing glasses
  4. Blue top and not wearing glasses

The answers are at the end of the article.

"Not" flips the answer

"Not" is the simplest of the three: it turns true into false and false into true. "Hands up if you are not wearing socks" means everyone who is barefoot.

Things get interesting when words are combined. "Not glasses and not blue top" is a different group from "not (glasses and blue top)". In the table above, the first rule picks only your child. The second picks everyone except Grandma. Older children enjoy finding pairs of rules that sound alike but pick different people.

Two paper switches and a light

Cut two small rectangles from card to be switches. On each one, draw a check mark on one side and a cross on the other. A check mark means the switch is on, a cross means it is off. Draw a bulb on a third card, colored yellow on one side and gray on the other.

Your child sets the two switches. Another player is "the computer" and must turn the bulb card to yellow or gray according to the rule of the day:

Switch 1 Switch 2 Rule "and" Rule "or"
on on light light
on off dark light
off on dark light
off off dark dark

A table like this, which lists every possible case, is called a truth table. Ask your child how many rows a table needs with three switches. (Eight: each new switch doubles the number of cases.)

Real circuits work the same way. When two switches sit one after the other on a wire, the current must pass both, so the bulb only shines when both are on: that is "and". When they sit side by side, either one lets the current through: that is "or". If your child builds circuits in a science class, they will see it for themselves.

Write the rules for an automatic door

Now your child becomes the programmer. Ask them to write a rule for a store door that opens by itself, using "and", "or" and "not". A first rule might be:

Open the door if someone is close.

Then play the tester and suggest tricky cases:

  • The store is closed for the night. Should the door still open? The rule becomes "someone is close and the store is not closed".
  • Someone is waiting inside to leave. The rule grows again: "(someone is close outside or someone is close inside) and the store is not closed".
  • A cleaner needs the door open while the store is closed. What could you add?

Each new case makes the rule better. Brackets show which words go together, just as they do in math. Your child can then write rules for other things around the house: "the porch light comes on if it is dark and something moves".

In Dologi, "Two rings" puzzles in the Logic topic use the same idea: each ring has its own label, things that fit both labels go in the middle where the rings overlap, and things that fit neither stay outside.

In short

  • A computer decides by checking conditions that are either true or false.
  • "And" needs both parts to be true; "or" needs at least one; "not" flips the answer.
  • A hands-up game, two paper switches and a truth table make these words concrete.
  • Writing and testing rules for an automatic door is how programmers improve a condition, one tricky case at a time.

Answers to the puzzle: 1. Grandma. 2. Mom, Grandma and your child (your child has both pets and still raises a hand). 3. Dad and your child. 4. Dad.

Read more: Sensor lights, kettles that switch off, doors that open by themselves: finding the hidden programs around your home