Introduction
Arithmetic gives you numbers. Boolean logic gives you yes or no — and a program that has to decide anything runs on yes and no.
There are three operators, and they combine yes-or-no answers into a single yes-or-no answer:
&&— AND. True only when both sides are true.||— OR. True when either side is true.!— NOT. Flips true to false and back.
You already know them in English. If the door is open and the alarm is armed. If it is raining or the ground is wet. If the button is not pressed. The operators are those sentences written down.
Two things get beginners. The symbols are doubled — && not &, || not | — because the single versions do something else entirely and compile without complaint. And these operators are lazy: && stops as soon as it finds a false, because nothing after it can change the answer.
What you will be able to do
By the end of this lesson you can:
- Combine conditions with
&&,||and!. - Fill in a truth table for each.
- Say why
&&is not&. - Explain short-circuit evaluation and why it matters.
- Read a compound condition aloud in plain English.
What you need
| Part | Type | Qty |
|---|---|---|
| Arduino UNO R3 | Microcontroller | 1 |
| USB A to B cable | Cable | 1 |
Basic
No wiring. Everything here prints to the Serial Monitor at 9600 baud.
A bool prints as 1 for true and 0 for false. That is how you see the answer without needing if yet.
The three operators
bool a = true;
bool b = false;
Serial.println(a && b); // 0 — AND: both must be true
Serial.println(a || b); // 1 — OR: either will do
Serial.println(!a); // 0 — NOT: flips it
Serial.println(!b); // 1
Truth tables
AND — &&
| a | b | a && b |
|---|---|---|
| false | false | false |
| false | true | false |
| true | false | false |
| true | true | true |
OR — ||
| a | b | a || b |
|---|---|---|
| false | false | false |
| false | true | true |
| true | false | true |
| true | true | true |
NOT — !
| a | !a |
|---|---|
| false | true |
| true |
Challenges
Challenge 1
Build the truth tables yourself.
Print all four combinations for &&, then all four for ||, then both for !. Label each line so the output reads:
false && false = 0
false && true = 0
true && false = 0
true && true = 1
Check what you printed against the tables in the lesson. They must match exactly.
Log in to ask for the answer.
Challenge 2
Say it in English.
For each of these, write the English sentence first, then print the result with a = true, b = false, c = true:
a && b
a || b
!a
a && !b
(a || b) && c
!(a && b)
!a || !b
The last two give the same answer for every combination. Try all four combinations of a and b and check.
That is a real rule with a name, and you have just proved it.
Log in to ask for the answer.
Challenge 3
Guard a division.
Set int total = 100; and int count = 0;.
- Print
total / count. Note what appears. - Now print
count != 0 && total / count > 10.
The second one does not misbehave, even though it contains the same division. Explain why in one sentence.
Then swap the two sides — total / count > 10 && count != 0 — and explain what changed.
Log in to ask for the answer.
Extra challenge
An alarm that makes sense.
An alarm should sound when all of these are true:
- the system is armed
- a door or a window is open
- it is not the test button being held
Write the single condition as one line of code, using three bool variables plus one for the window, and print the result.
Then test it against every case that matters: armed with the door open, armed with nothing open, not armed with the door open, and armed with the door open while the test button is held.
Think about it: you tested four cases. With four bool variables there are sixteen possible combinations. How would you be sure about the other twelve — and is testing all sixteen always possible when a condition has ten inputs?
Log in to ask for the answer.