Yes or no
After this: Write a comparison and say whether its answer is yes or no.
On this page · 6 sections · 3 checks
Every day you make small decisions by asking a question. Is it raining? If yes, take an umbrella. Is the shop open? If yes, go in. The question has only two answers, yes or no, and the answer decides what you do next.
A program makes decisions the same way. Before it can choose, it needs a way to ask a question and get a yes or a no. That is this lesson. The next lesson uses the answer to choose between two paths.
A question with a yes or no answer
In "Your first program" you met the two yes/no values, Truetrue and Falsefalse. The computer can work one out for you by comparing two values.
- Is 5 bigger than 3? Yes. The computer writes that question as
5 > 3, and its answer is Truetrue. - Is 72 at least 80? No. The question is
72 >= 80, and the answer is Falsefalse.
A question that the computer answers with a yes/no value is called a . A condition that compares two values is called a .
The six comparison signs
| Sign | The question it asks | Example | Answer |
|---|---|---|---|
> | bigger than? | 5 > 3 | Truetrue |
< | smaller than? | 5 < 3 | Falsefalse |
>= | bigger than, or the same as? | 72 >= 80 | Falsefalse |
<= | smaller than, or the same as? | 72 <= 80 | Truetrue |
== | the same as? | 72 == 72 | Truetrue |
!= | different from? | 72 != 72 | Falsefalse |
Read >= as "at least" and <= as "at most". The signs made of two characters are typed with no space between them.
Watch it happen
The program below asks four questions and prints each answer. The last two questions use a variable, score, so the computer puts the value 72 in place of the name before it answers. Press Run it and step through.
- Run the programRun the program and step through it. Each print line asks a question and prints its answer.
print(5 > 3)print(72 >= 80)score = 72print(score >= 70)print(score == 72)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
Python runs in the browser when you press Run on the block below. Java does not run here: if you are learning Java, step through the trace above instead, and paste the code into a Java tool on your computer when you want to run it for real.
Change 72 to 80 and run again. Which answers change?
A question is not an instruction
Two signs look alike and are mixed up by almost everyone at first.
=is an instruction: put this value into this box.answer = 4makes the boxanswerhold 4. It asks nothing.==is a question: are these two values the same?answer == 4looks in the box and answers yes or no. It changes nothing.
Step through this program. Watch the box change on line 35, and watch the answer to the same question change with it.
answer = 4print(answer == 4)answer = 5print(answer == 4)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
A rule that helps: a comparison never changes a box, and an instruction never gives an answer. When you want the computer to check something, you need two equals signs.
Check yourself
What is the answer to the condition 72 >= 80?
The box answer already exists. One of answer = 4 and answer == 4 is a question and the other is an instruction. Which is which?
What does this print?
age = 20print(age > 18)print(age == 18)int age = 20;System.out.println(age > 18);System.out.println(age == 18);Honesty notes
- Say what a condition is: a question the computer answers with True or False (true or false in Java).
- Read the six comparison signs, including >= as at least and <= as at most.
- Tell = (an instruction: put this value in the box) from == (a question: are these the same?).
- Predict what a print of a comparison shows.