AP Computer Science Principles · Unit 3 lecture · Part 5 of 14 · 9 min

3.6 and 3.7 Conditionals and nested conditionalsChoosing a path

Mr. Thompkins
Now we put Booleans to work. Selection is the second of the three ingredients of every algorithm: the program looks at a condition and chooses what to do.

Selection determines which parts of an algorithm run, based on whether a condition is true or false. In code it takes the form of conditional statements, often called if-statements. IF (condition) { block } runs the block only when the condition is true; otherwise it skips it. IF (condition) { first } ELSE { second } runs exactly one of the two blocks: the first when the condition is true, the second when it's false.

Conditionals change the normal top-to-bottom flow of control. And nested conditional statements, conditionals inside other conditionals, let a program make a decision that depends on an earlier decision. Here's a museum's ticket price: free under 5, 8 dollars for 65 and older, 12 dollars for everyone else.

IF (age < 5)truefalseprice ← 0IF (age ≥ 65)truefalseprice ← 8price ← 12exactly one boxof the three runs
A nested conditional. The inner IF is only reached when the outer condition is false. Exactly one price is assigned.
1IF (age < 5)2{3  price ← 04}5ELSE6{7  IF (age ≥ 65)18  {9    price ← 810  }11  ELSE12  {13    price ← 1214  }15}16DISPLAY(price)
  1. line 7 A nested conditional: checked only when age is not under 5
The same decision in pseudocode.
ageage < 5?age ≥ 65?price
4truenot checked0
5falsefalse12
64falsefalse12
65falsetrue8
Test at the boundaries: 4 and 5, 64 and 65.
?Wait, what's a boundary value?
From Unit 1: the values right at, just below and just above a cutoff. Conditionals are where they matter most, because > versus ≥ only makes a difference at the cutoff itself.

Watch out for separate IF statements that aren't connected by ELSE. Each one is checked, and every true one runs, in order. That means the order can change the result. This grading code looks reasonable, but trace a 95.

1grade ← "F"2IF (score ≥ 90)13{4  grade ← "A"5}6IF (score ≥ 80)17{8  grade ← "B"9}10IF (score ≥ 70)111{12  grade ← "C"13}14DISPLAY(grade)
  1. line 2-10 Three separate IFs: every true one runs, in order
Three separate IFs. For a score of 95, all three conditions are true.

For score 95: the first IF sets grade to "A". The second IF is also true, so grade becomes "B". The third is also true, so grade becomes "C". It displays C for a 95. The later IFs overwrite the earlier ones. Two fixes work: put the IFs in the opposite order (70, then 80, then 90, so the strictest one runs last), or connect them with ELSE so only the first true branch runs.

?Wait, does ELSE need its own condition?
No. ELSE has no condition. It runs exactly when the IF's condition was false. If you need another test, nest another IF inside the ELSE, as the ticket code does.
Pause and think
In the grading code, what grade does a score of 72 get? What about 85?
I thought about it. Show me
72: only the third IF is true, so "C", which is correct. 85: the second and third are true, so "B" then "C": it displays "C", which is wrong. The bug only shows for scores of 80 and above.
Which inputs reach which branch?
This code chooses what to wear. For each pair of inputs, give the output: (a) temp 45, raining false (b) temp 45, raining true (c) temp 70, raining false. Then (d) describe every input that displays "coat".
1IF (raining)2{3  wear ← "umbrella"4}5ELSE6{7  IF (temp < 50)8  {9    wear ← "coat"10  }11  ELSE12  {13    wear ← "t-shirt"14  }15}16DISPLAY(wear)
A nested conditional.
  1. (a) raining is false, so go to the ELSE. 45 < 50 is true, so wear is "coat".
    Why?
    The outer condition is checked first. The inner IF only runs inside the ELSE.
  2. (b) raining is true, so wear is "umbrella". The temperature is never checked.
  3. (c) raining is false; 70 < 50 is false, so the inner ELSE runs: "t-shirt".
  4. (d) "coat" needs both: not raining, and temp below 50. As a Boolean: (NOT raining) AND (temp < 50).
    Why?
    A nested conditional can always be described by combining its conditions with AND.

Answer: (a) coat (b) umbrella (c) t-shirt (d) not raining and temp below 50.

Stuck? Get a hint
  1. Start with the outer IF: is it raining?
  2. Only when it isn't raining does the temperature matter.
  3. For (d), list both conditions that must hold.
Check yourself
What is displayed when this code segment runs with n ← 12?
IF (n MOD 3 = 0)
{
  n ← n + 1
}
IF (n MOD 2 = 0)
{
  n ← n * 10
}
ELSE
{
  n ← n - 1
}
DISPLAY(n)
  1. A120
  2. B130
  3. C12
  4. D13
Show the answer

C. 12 MOD 3 = 0, so n becomes 13. Now the second IF tests the new n: 13 MOD 2 = 1, so the ELSE runs and n becomes 12. 120 is the trap: it tests the second condition with the old n.

  • IF runs its block only when the condition is true. IF/ELSE runs exactly one of two blocks.
  • Nested conditionals: an IF inside an IF or ELSE, for decisions that depend on earlier ones.
  • Separate IFs are each checked; a later true IF can overwrite an earlier one. Order matters.
  • Test conditionals at every boundary.