Multiple alternatives with nested if blocks

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Multiple alternatives with nested if blocks

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A program needs to choose between THREE different messages. Which setup works in a visual editor?
A An if-else block placed inside the else part of another if-else block
B One if-else block with three conditions and no other blocks
2. How many alternatives can ONE if-else block with nothing inside it choose between?
A Three
B Two
3. Score bands: score > 79 says 'A', score > 49 says 'B', otherwise says 'C'. Which check should go in the OUTER if?
A score > 79
B score > 49
4. A student's score is 85. The outer if checks score > 49 first and says 'B'. What goes wrong?
A The program says 'B' when it should say 'A', because the order of checks is wrong
B The program says 'A' because it checks every block at once
5. In the nested blocks, when does the INNER if-else run?
A Every time, before the outer condition is checked
B Only when the outer condition is false

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1if score > 89 then say A else (if score > 69 then say B else say C) Aif score > 89 then A else (if score > 69 then B else C)
2else (if score > 49 then say Pass else say Try again) Bif score > 89 then say A else (if score > 69 then say B else say C)
3The order of checks matters: check the highest score band first so a score of 95 gets an A and not a B. CThe second if block is placed after the else block instead of inside the else part, so there is no real third choice and the program can't choose properly between three alternatives.
4A nested if-else is the same as the algorithm's ELSE IF: IF score > 89 THEN A, ELSE IF score > 69 THEN B, ELSE C. DAn if-else block sits inside the else part of another if-else block, so the program can choose between three results: A, B or C.
5if score > 69 then say B else (if score > 89 then say A else say C) EIf the first check fails, the program moves on to this next check and picks one of two new answers.
6if score > 89 then say A else say B (if score > 69 then say C) FThe checks are in the wrong order. A score of 95 is more than 69, so it says B and never reaches the check for A.

Definitions

nested if-else

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
if score > 79 then say("A") else __ score > 49 then say("B") else say("C")
2.
if score _ 79 then say("A") else if score > 49 then say("B") else say("C")
3.
if _____ > 79 then say("A") else if score > 49 then say("B") else say("C")
4.
if score > 79 then say("A") else if score > 49 then ___("B") else say("C")
5.
if score > 79 then say("A") else if score > 49 then say("B") ____ say("C")

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A if score > 89 then
B say B
C else
D if score > 79 then say B
E else say C
Question 2
A if temp > 29 then
B say Hot
C else
D if temp < 19 then say Warm
E else say Cold
Question 3
A if score > 59 then say A
B else
C if score > 79 then say B
D else say C
Question 4
A if age < 5 then say Free
B else
C if age < 13 say Child
D else say Adult
Question 5
A if stars = 3 then say Great
B else
C if stars = 3 then say Good
D else say Try again

Answer Key

Multiple alternatives with nested if blocks
Section A: Quick Questions
Q1
A — An if-else block placed inside the else part of another if-else block
Q2
B — Two
Q3
A — score > 79
Q4
A — The program says 'B' when it should say 'A', because the order of checks is wrong
Q5
B — Only when the outer condition is false
Section B: Match the Pairs
Q1
if score > 89 then say A else (if score > 69 then say B else say C) → An if-else block sits inside the else part of another if-else block, so the program can choose between three results: A, B or C.
Q2
else (if score > 49 then say Pass else say Try again) → If the first check fails, the program moves on to this next check and picks one of two new answers.
Q3
The order of checks matters: check the highest score band first so a score of 95 gets an A and not a B. → if score > 89 then A else (if score > 69 then B else C)
Q4
A nested if-else is the same as the algorithm's ELSE IF: IF score > 89 THEN A, ELSE IF score > 69 THEN B, ELSE C. → if score > 89 then say A else (if score > 69 then say B else say C)
Q5
if score > 69 then say B else (if score > 89 then say A else say C) → The checks are in the wrong order. A score of 95 is more than 69, so it says B and never reaches the check for A.
Q6
if score > 89 then say A else say B (if score > 69 then say C) → The second if block is placed after the else block instead of inside the else part, so there is no real third choice and the program can't choose properly between three alternatives.
Section C: Definitions
Q1
nested if-else — An if-else block placed inside the else part of another if-else block. The program checks one condition at a time, in order, so it can choose between three or more alternatives.
Section D: Fill in the Blanks
Q1
if score > 79 then say("A") else if score > 49 then say("B") else say("C")
Q2
if score > 79 then say("A") else if score > 49 then say("B") else say("C")
Q3
if score > 79 then say("A") else if score > 49 then say("B") else say("C")
Q4
if score > 79 then say("A") else if score > 49 then say("B") else say("C")
Q5
if score > 79 then say("A") else if score > 49 then say("B") else say("C")
Section E: Spot the Error
Q1
B — The first check is for scores over 89, so this branch must say A, the top grade. It should be say A.
Q2
D — The inner check uses < so it would say Warm for cold days. It should be temp > 19 to match the band between 20 and 29.
Q3
A — The first check must be the highest band. With > 59, scores like 70 would say A and the later checks would never run. It should be score > 89.
Q4
C — The word then is missing. Every if block needs a condition followed by then. It should be if age < 13 then say Child.
Q5
C — The program already checked for 3 stars in the first if, so this check can never be true. It should check stars = 2.