Introduction
You have been using these since the last lesson without a proper introduction. A comparison operator takes two values and answers true or false — which is exactly what if needs.
There are six, and five of them are obvious. The sixth is ==, and it is the single most common typo in C++, because it looks so much like = and means something completely different.
=puts a value into a variable. It is an instruction.==asks whether two values are the same. It is a question.
if (x = 5) compiles cleanly, changes x, and is always true. if (x == 5) asks. One character.
The other thing worth knowing early: never use == on a decimal. 0.1 + 0.2 is not exactly 0.3 inside the chip, so a comparison that ought to be true is false. Compare a range instead.
What you will be able to do
By the end of this lesson you can:
- Use all six comparison operators correctly.
- Say the difference between
=and==without hesitating. - Choose between
>and>=at a boundary. - Explain why
!=is often clearer than!(a == b). - Say why comparing floats with
==is unreliable, and what to do instead.
What you need
| Part | Type | Qty |
|---|---|---|
| Arduino UNO R3 | Microcontroller | 1 |
| USB A to B cable | Cable | 1 |
Basic
No wiring. Read everything in the Serial Monitor at 9600 baud.
The six
| Operator | Asks | a = 7, b = 2 |
|---|---|---|
== | are they equal? | false |
!= | are they different? | true |
> | is a bigger? | true |
< | is a smaller? | false |
>= | bigger or the same? | true |
<= | smaller or the same? | false |
Each one produces a bool, which prints as 1 or 0:
int a = 7;
int b = 2;
Serial.println(a == b); // 0
Serial.println(a != b); // 1
Serial.println(a > b); // 1
Serial.println(a >= 7); // 1
= against ==
int x = 3;
if (x == 5) { ... } // asks: is x five? — false
if (x = 5) { ... } // sets x to 5, then: is 5 true? — yes, always
The second one is legal C++, so nothing warns you. Two things go wrong at once: the branch always runs, and has been changed behind your back.
Challenges
Challenge 1
All six, all outcomes.
With int a = 7; and int b = 7;, print all six comparisons with labels.
Then change b to 2 and run again. Then b to 12.
You should end with three sets of six results. Which operators changed answer between the first and second run, and which did not?
Log in to ask for the answer.
Challenge 2
The boundary.
A prize needs a score of 100 or more.
Write the test with > first and check scores of 99, 100 and 101. Then write it with >= and check the same three.
Exactly one score gives a different answer. Which, and which version is correct for the requirement as written?
Now rewrite the requirement so that > would be the correct operator, without changing who wins.
Log in to ask for the answer.
Challenge 3
Three that lie.
Each of these compiles and none does what it appears to. Predict the output, then run it.
int x = 3;
if (x = 5) {
Serial.println("A");
}
Serial.println(x);
float f = 0.1 + 0.2;
if (f == 0.3) {
Serial.println("B");
}
int y = 50;
if (0 < y < 10) {
Serial.println();
}
Extra challenge
A guessing game.
Pick a secret number with random(1, 101) in setup() and print nothing about it.
Then in loop(), have the sketch guess by counting upward from 1, once every 200 ms, printing:
too lowwhile the guess is below the secretgot it: nwhen they match, and then stop counting- it should never print
too high
Use ==, < and an if chain.
Then change the secret to a float like 42.5 and watch the game never finish. Explain why in one sentence — and fix it with a range test.
Think about it: counting upward from 1 takes up to 100 guesses. If you could ask "higher or lower" each time and jump to the middle, how many guesses would 1 to 100 need at worst? Try it on paper.
Log in to ask for the answer.