Article
When an if can become one Boolean
Look at this code. The IF takes eight lines to copy one yes-or-no answer into a variable. There is a one-line version that does exactly the same thing.
age ← 20
IF(age ≥ 18)
{
adult ← true
}
ELSE
{
adult ← false
}
DISPLAY(adult)An IF that sets true or false
The condition age ≥ 18 is already true or false. So the whole IF only copies the condition's value into adult. You can store the value directly:
age ← 20 adult ← age ≥ 18 DISPLAY(adult)
Both programs display true. Here are both in Python, one after the other. Change age to 15 and run again. The two lines always match.
There are three common patterns, and each one has a one-line form:
| The code | One-line form |
|---|---|
IF(c) { x ← true } ELSE { x ← false } | x ← c |
IF(c) { x ← false } ELSE { x ← true } | x ← NOT c |
x ← false then IF(c) { x ← true } | x ← c |
In the second pattern the branches are swapped, so you add a NOT. In the third pattern x starts as false and only becomes true when the condition is true. The result is the same as x ← c.
Check your understanding
Which single statement does the same as this code?
IF(x > 10)
{
big ← true
}
ELSE
{
big ← false
}An IF inside an IF is an AND
When the inner IF is the only thing inside the outer IF, and neither has an ELSE, the code runs only when both conditions are true:
IF(age ≥ 13)
{
IF(age ≤ 19)
{
DISPLAY("teen")
}
}This is equivalent to one IF with AND:
IF(age ≥ 13 AND age ≤ 19)
{
DISPLAY("teen")
}Be careful with two IFs one after the other. They are not an OR. Run this program and compare the two parts:
The two separate IFs both pass for x = 5, so ok is displayed twice. The single IF with OR displays it once. Two separate IFs and one IF with OR only match when the two conditions can never be true at the same time.
An IF with an ELSE that holds another IF does act like OR:
IF(a)
{
DISPLAY("go")
}
ELSE
{
IF(b)
{
DISPLAY("go")
}
}This displays go once if a is true, once if only b is true, and never if both are false. It is equivalent to IF(a OR b).
Check your understanding
Both IFs in this code can be replaced by one IF. Which condition makes hi appear in exactly the same cases?
IF(a)
{
IF(b)
{
DISPLAY("hi")
}
}Worked example
Which condition makes this code display go in the same cases?
IF(a)
{
IF(b)
{
DISPLAY("go")
}
}- I.
IF(a AND b) - II.
IF(NOT ((NOT a) OR (NOT b))) - III.
IF(a OR b)
Make a table of when go is displayed:
| a | b | Original | I | II | III |
|---|---|---|---|---|---|
| true | true | go | go | go | go |
| true | false | nothing | nothing | nothing | go |
| false | true | nothing | nothing | nothing | go |
| false | false | nothing | nothing | nothing | nothing |
For II: NOT ((NOT a) OR (NOT b)) is the same as a AND b by De Morgan's rule. In the first row it is NOT (false OR false), which is true. In the second row it is NOT (false OR true), which is false.
I and II match the original in every row. III fails in two rows. The answer is I and II.
Common mistakes
- Treating two IFs in a row as an OR. When both conditions are true, the body runs twice.
- Treating a nested IF that has an ELSE as a plain AND. The rule only works when the IF inside is the only thing inside, and nothing has an ELSE.
- Missing the NOT when the branches are swapped. If the IF sets
falseand the ELSE setstrue, the one-line form isx ← NOT c. - Storing the wrong thing.
adult ← agestores the number, nottrueorfalse.
On the exam
Questions give a code segment and ask which other segment "displays the same result" or "has the same behaviour for all inputs". Make a small table with every combination of the conditions, like the one above. Check whether any segment runs its body a different number of times from the original.
Key words
Boolean variable: a variable that holds true or false.
Nested conditional: an IF placed inside another IF. When nothing else is inside and there is no ELSE, it acts like AND.