Conditionals
How a program chooses which steps to run: selection in plain words, IF and IF-ELSE statements, and how to trace them with more than one input.
1Selection
Selection decides which parts of an algorithm run, based on whether a condition is true or false. You use it every day without code:
Check the forecast. If rain is forecast, pack an umbrella; otherwise, pack sunglasses. Leave for school.
Three things to notice. The condition, “rain is forecast”, is a yes-or-no question. Only one of the two middle steps happens on any morning. And the steps before and after the choice, checking the forecast and leaving, happen every time.
Selection asks its question once, each time the algorithm reaches it. Asking the same question over and over until something changes is iteration, a different building block.
2IF
Conditional statements, also called if-statements, change the normal top-to-bottom order of a program by running different statements depending on a Boolean expression.
IF (condition)
{
<block of statements>
}
The block runs if condition is true. If it is false, no action is taken and the program continues after the closing brace.
price ← 40
IF (items > 10)
{
price ← price - 5
}
DISPLAY(price)
With items equal to 12, the condition is true and this displays 35. With 10, 10 > 10 is false, so the price stays at 40.
3IF with ELSE
IF (condition)
{
<first block of statements>
}
ELSE
{
<second block of statements>
}
The first block runs if condition is true; otherwise the second block runs. One block always runs, and never both.
IF (items > 10)
{
shipping ← 0
}
ELSE
{
shipping ← 7
}
DISPLAY(shipping)
| items | items > 10 | Block that runs | Displays |
|---|---|---|---|
| 12 | true | first | 0 |
| 10 | false | ELSE | 7 |
| 3 | false | ELSE | 7 |
Trace a conditional by writing the condition, its value, and then only the statements of the block that runs. Test the boundary: 10 items is not more than 10, so it pays shipping. If the condition were items ≥ 10, 10 items would ship free.
IF runs its block only when the condition is true and otherwise does nothing. IF with ELSE runs the first block when the condition is true and the second block when it is false, so one of them always runs.
4Two IFs are not one IF-ELSE
Two separate IF statements each test their own condition, so both blocks can run, or neither. An IF-ELSE always runs one block.
grade ← "none"
IF (score ≥ 50)
{
grade ← "pass"
}
IF (score ≥ 80)
{
grade ← "merit"
}
DISPLAY(grade)
IF (score ≥ 80)
{
grade ← "merit"
}
ELSE
{
grade ← "pass"
}
DISPLAY(grade)
| score | Two IFs display | IF-ELSE displays |
|---|---|---|
| 90 | merit | merit |
| 60 | pass | pass |
| 20 | none | pass |
At 90, both IFs run in the first version; the second one overwrites pass with merit, so the results match. At 20 they differ: neither IF runs in the first version, but the ELSE in the second version catches every score below 80, including scores that should not pass. To compare two segments, test a value both conditions accept and a value only the ELSE would catch.
5Worked examples
Even or odd
What is displayed when n is 7?
IF (n MOD 2 = 0)
{
DISPLAY("even")
}
ELSE
{
DISPLAY("odd")
}
DISPLAY("done")
- 1
7 MOD 2is 1, so7 MOD 2 = 0is false. - 2The ELSE block runs and displays
odd. - 3
DISPLAY("done")is after the IF-ELSE, so it runs either way.
odd then doneWhen does it display B?
For which whole numbers x from 0 to 12 does this display B?
result ← "A"
IF (x < 4 OR x > 9)
{
result ← "B"
}
DISPLAY(result)
- 1It displays B only when the IF block runs, which needs
x < 4 OR x > 9to be true. - 2Test the edges:
x Displays 3 B 4 A 9 A 10 B
Free shipping
Orders of $35 or more ship free; smaller orders pay a $6 fee. Write a segment that displays the amount charged, and test it.
- 1“35 or more” is
total ≥ 35. Every order either qualifies or pays, so use IF-ELSE.IF (total ≥ 35) { fee ← 0 } ELSE { fee ← 6 } DISPLAY(total + fee) - 2Test the boundary and one value on each side:
total Displays 35 35 34 40 50 50
Same result?
Which of these always display the same value as Segment A?
Segment A
IF (x > 5)
{
y ← 1
}
ELSE
{
y ← 2
}
DISPLAY(y)
Segment B
y ← 2
IF (x > 5)
{
y ← 1
}
DISPLAY(y)
Segment C
IF (x > 5)
{
y ← 1
}
IF (x < 5)
{
y ← 2
}
DISPLAY(y)
- 1Segment B sets
yto 2 first and changes it to 1 only whenx > 5, the same as A. - 2Segment C has two IFs. When
xis 5, neither condition is true, soynever gets a value and the program stops with an error. - 3Test 4, 5, and 6:
x A B C 4 2 2 2 5 2 2 error: yhas no value6 1 1 1
6Common mistakes
In an IF-ELSE, one block runs, never both. Cross out the block that does not run before you trace further.
An IF with no ELSE does nothing when its condition is false. Variables keep the values they had.
Separate IFs are each tested, so both blocks can run, or neither.
items > 10 is false at 10. Always trace the value where the condition changes.
“When items is 10, the condition items > 10 is false, so the ELSE block runs and sets shipping to 7. The first block is skipped. The program displays 7.”
Give the condition’s value for the input, say which block runs and which is skipped, and give the result.