3.6 and 3.7 Conditionals and nested conditionalsChoosing a path

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.
1IF (age < 5)2{3 price ← 04}5ELSE6{7 IF (age ≥ 65)18 {9 price ← 810 }11 ELSE12 {13 price ← 1214 }15}16DISPLAY(price)
- line 7 A nested conditional: checked only when age is not under 5
| age | age < 5? | age ≥ 65? | price |
|---|---|---|---|
| 4 | true | not checked | 0 |
| 5 | false | false | 12 |
| 64 | false | false | 12 |
| 65 | false | true | 8 |
?Wait, what's a boundary value?
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)
- line 2-10 Three separate IFs: every true one runs, in order
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?
I thought about it. Show me
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) 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. - (b) raining is true, so wear is "umbrella". The temperature is never checked.
- (c) raining is false; 70 < 50 is false, so the inner ELSE runs: "t-shirt".
- (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
- Start with the outer IF: is it raining?
- Only when it isn't raining does the temperature matter.
- For (d), list both conditions that must hold.
IF (n MOD 3 = 0)
{
n ← n + 1
}
IF (n MOD 2 = 0)
{
n ← n * 10
}
ELSE
{
n ← n - 1
}
DISPLAY(n)- A120
- B130
- C12
- 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.