Article
Booleans, comparisons and else
In the last article, a condition like temp > 25 was always true or false. These two answers are values, just like numbers and strings, and a program can store them. This article names them, lists every comparison you can make, and adds a second route with ELSE.
Booleans: true or false
A Boolean value is either true or false. Nothing else. It is the third data type you have met, after numbers and strings. A Boolean expression is any expression whose result is a Boolean, such as x > 5.
x ← 7 DISPLAY(x > 5) DISPLAY(x = 3)
The first line shows true because 7 is greater than 5. The second shows false. Together they appear as true false.
You can store a Boolean in a variable, and use that variable as the condition:
isRaining ← true
IF(isRaining)
{
DISPLAY("Take an umbrella")
}Python writes the two values with capital letters: True and False. And watch the quotes. "true" in quotes is a string, not a Boolean.
The six comparisons
A relational operator compares two values and gives a Boolean. There are six of them. The AP pseudocode symbols are on the exam's reference sheet. Python writes some of them differently.
| AP | Python | Means | Result when x is 5 |
|---|---|---|---|
x = 5 | x == 5 | equal to | true |
x ≠ 5 | x != 5 | not equal to | false |
x > 5 | x > 5 | greater than | false |
x < 5 | x < 5 | less than | false |
x ≥ 5 | x >= 5 | greater than or equal to | true |
x ≤ 5 | x <= 5 | less than or equal to | true |
The biggest trap is =. In AP pseudocode, = asks if two values are equal and ← stores a value. In Python it is the other way round: = stores and == asks.
Run it, then change x to 6 and to 4. For each value, check the six answers against the table.
The comparisons work on text as well. "cat" = "cat" is true. Capital letters count, so "Cat" = "cat" is false. And a number never equals a string: 5 = "5" is false.
Now the boundary. The only difference between > and ≥ is what happens when the two values are equal:
| score | score > 50 | score ≥ 50 |
|---|---|---|
| 49 | false | false |
| 50 | false | true |
| 51 | true | true |
Whenever a question uses a value that sits exactly on the edge, slow down and check which symbol it has.
Check your understanding
What is displayed when this line runs?
DISPLAY(8 ≥ 8)
ELSE: two routes
An IF skips its block when the condition is false. Often you want to do something else instead. ELSE gives the other route. Exactly one of the two blocks runs, never both and never neither.
age ← 16
IF(age ≥ 18)
{
DISPLAY("Can vote")
}
ELSE
{
DISPLAY("Too young")
}16 is not at least 18, so the condition is false and the ELSE block runs. The output is Too young. Try age = 18 and see the boundary at work.
ELSE has no condition of its own. It simply means "in every other case".
Check your understanding
What is displayed when this program runs?
n ← 4
IF(n MOD 2 = 0)
{
DISPLAY("even")
}
ELSE
{
DISPLAY("odd")
}Worked example: a password
secret ← "owl"
guess ← "Owl"
IF(guess = secret)
{
DISPLAY("Welcome")
}
ELSE
{
DISPLAY("Wrong")
}| Step | Result |
|---|---|
guess = secret means "Owl" = "owl" | false (capital O is not the same as o) |
| Run which block? | the ELSE block |
| Displayed | Wrong |
Common mistakes
- Using
=to store in Python, or==to store in AP. In AP,=compares and←stores. In Python,=stores and==compares. - Mixing up
>and≥. They differ only when the values are equal. - Running both blocks of an IF/ELSE. Only one ever runs.
- Giving ELSE a condition. There is none. It is just
ELSE. - Writing
"true"when you meantrue. The first is a string. - Forgetting capitals.
"Yes"and"yes"are not equal.
On the exam
Expect questions that give values and ask whether a condition is true, which of two blocks runs, or what is displayed. Pay special attention to values on the boundary, such as age ≥ 18 when age is exactly 18. The exam always writes = for equal and ← for storing.
Key words
Boolean: a value that is either true or false.
Boolean expression: an expression whose result is a Boolean.
Relational operator: a symbol that compares two values: =, ≠, >, <, ≥, ≤.
ELSE: the block that runs when the IF's condition is false.