Unit 3: Programming · Conditionals

Article

Booleans, comparisons and else

7 min read

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.

AP pseudocode
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:

AP pseudocode
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.

APPythonMeansResult when x is 5
x = 5x == 5equal totrue
x ≠ 5x != 5not equal tofalse
x > 5x > 5greater thanfalse
x < 5x < 5less thanfalse
x ≥ 5x >= 5greater than or equal totrue
x ≤ 5x <= 5less than or equal totrue

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.

Runs in your browser

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:

scorescore > 50score ≥ 50
49falsefalse
50falsetrue
51truetrue

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?

AP pseudocode
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.

AP pseudocode
age ← 16
IF(age ≥ 18)
{
    DISPLAY("Can vote")
}
ELSE
{
    DISPLAY("Too young")
}
Runs in your browser

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?

AP pseudocode
n ← 4
IF(n MOD 2 = 0)
{
    DISPLAY("even")
}
ELSE
{
    DISPLAY("odd")
}

Worked example: a password

AP pseudocode
secret ← "owl"
guess ← "Owl"
IF(guess = secret)
{
    DISPLAY("Welcome")
}
ELSE
{
    DISPLAY("Wrong")
}
StepResult
guess = secret means "Owl" = "owl"false (capital O is not the same as o)
Run which block?the ELSE block
DisplayedWrong

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 mean true. 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.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.