Choosing between several paths

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

Choosing between several paths

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does ELSE IF do in an algorithm?
A Checks another condition when the first one is false
B Repeats the steps again and again
2. An algorithm has IF, ELSE IF and ELSE. How many paths can it choose from?
A Only two paths
B Three paths
3. IF it is raining THEN take an umbrella. ELSE IF it is sunny THEN wear a hat. It is raining and sunny at once. What happens?
A Take an umbrella AND wear a hat, because both are true
B Take an umbrella, because the first true condition is the one that runs
4. In what order does the algorithm check the conditions?
A In order, from the top down
B All at the same time
5. What is the job of the final ELSE?
A A catch-all that runs when none of the conditions are true
B It checks one more condition before it runs

Match the Pairs

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

Left columnRight column
1ELSE IF temperature < 25 THEN AA catch-all path that runs when none of the conditions above were true
2ELSE Wear a t-shirt BIF temp > 30 THEN Stay inside ELSE IF temp > 20 THEN Go to the park ELSE Wear a jacket
3Conditions are checked in order from the top, and only the first true one runs CThe ELSE is before the ELSE IF. The catch-all ELSE must be last, otherwise the ELSE IF can never be checked.
4Using ELSE IF lets an algorithm choose between three paths: hot, mild or cold DIF score >= 90 THEN Show A ELSE IF score >= 70 THEN Show B
5IF mark >= 80 THEN Say Great ELSE IF Say Good job EChecks a second condition only when the first condition was false
6IF age < 5 THEN Say Toddler ELSE Say Child ELSE IF age < 13 THEN Say Kid FThe ELSE IF has no condition. It needs one, such as ELSE IF mark >= 50 THEN.

Definitions

ELSE IF

Fill in the Blanks

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

1.
IF temp > 30 THEN DISPLAY "Hot" _______ temp > 20 THEN DISPLAY "Warm" ELSE DISPLAY "Cold"
2.
IF mark >= 80 THEN grade ← "A" ELSE IF mark >= 50 THEN grade ← "B" ____ grade ← "C"
3.
IF light = "red" THEN DISPLAY "Stop" ELSE IF light = "yellow" ____ DISPLAY "Slow" ELSE DISPLAY "Go"
4.
IF coins >= 10 THEN DISPLAY "Big" ELSE IF coins __ 5 THEN DISPLAY "Medium" ELSE DISPLAY "Small"
5.
IF weather = "rain" THEN DISPLAY "Coat" ELSE __ weather = "sun" THEN DISPLAY "Hat" ELSE DISPLAY "Jumper"

Spot the Error

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

Question 1
A SET score TO 95
B IF score >= 50 THEN
C SAY "Gold"
D ELSE IF score >= 50 THEN
E SAY "Silver"
F ELSE SAY "Bronze"
Question 2
A IF weather = "rainy"
B SAY "Take an umbrella"
C ELSE IF weather = "sunny" THEN
D SAY "Wear sunscreen"
E ELSE
F SAY "Wear a jacket"
Question 3
A IF light = "red" THEN
B SAY "Stop"
C ELSE light = "yellow" THEN
D SAY "Slow down"
E ELSE
F SAY "Go"
Question 4
A SET speed TO 45
B IF speed < 60 THEN
C SAY "Too fast"
D ELSE IF speed > 40 THEN
E SAY "Just right"
F ELSE SAY "Too slow"
Question 5
A IF pet = "dog" THEN
B SAY "Take it for a walk"
C IF pet = "fish" THEN
D SAY "Clean the tank"
E ELSE
F SAY "Give it a cuddle"

Answer Key

Choosing between several paths
Section A: Quick Questions
Q1
A — Checks another condition when the first one is false
Q2
B — Three paths
Q3
B — Take an umbrella, because the first true condition is the one that runs
Q4
A — In order, from the top down
Q5
A — A catch-all that runs when none of the conditions are true
Section B: Match the Pairs
Q1
ELSE IF temperature < 25 THEN → Checks a second condition only when the first condition was false
Q2
ELSE Wear a t-shirt → A catch-all path that runs when none of the conditions above were true
Q3
Conditions are checked in order from the top, and only the first true one runs → IF score >= 90 THEN Show A ELSE IF score >= 70 THEN Show B
Q4
Using ELSE IF lets an algorithm choose between three paths: hot, mild or cold → IF temp > 30 THEN Stay inside ELSE IF temp > 20 THEN Go to the park ELSE Wear a jacket
Q5
IF mark >= 80 THEN Say Great ELSE IF Say Good job → The ELSE IF has no condition. It needs one, such as ELSE IF mark >= 50 THEN.
Q6
IF age < 5 THEN Say Toddler ELSE Say Child ELSE IF age < 13 THEN Say Kid → The ELSE is before the ELSE IF. The catch-all ELSE must be last, otherwise the ELSE IF can never be checked.
Section C: Definitions
Q1
ELSE IF — Checks another condition when the first one is false, so an algorithm can pick from more than two paths. Conditions are checked in order and only one path runs.
Section D: Fill in the Blanks
Q1
IF temp > 30 THEN DISPLAY "Hot" ELSE IF temp > 20 THEN DISPLAY "Warm" ELSE DISPLAY "Cold"
Q2
IF mark >= 80 THEN grade ← "A" ELSE IF mark >= 50 THEN grade ← "B" ELSE grade ← "C"
Q3
IF light = "red" THEN DISPLAY "Stop" ELSE IF light = "yellow" THEN DISPLAY "Slow" ELSE DISPLAY "Go"
Q4
IF coins >= 10 THEN DISPLAY "Big" ELSE IF coins >= 5 THEN DISPLAY "Medium" ELSE DISPLAY "Small"
Q5
IF weather = "rain" THEN DISPLAY "Coat" ELSE IF weather = "sun" THEN DISPLAY "Hat" ELSE DISPLAY "Jumper"
Section E: Spot the Error
Q1
B — Conditions are checked in order. A score of 95 is at least 50, so it would get Silver and never reach the Gold check. The line should be IF score >= 90 THEN.
Q2
A — The IF line is missing the word THEN. It should be IF weather = "rainy" THEN.
Q3
C — To check another condition you need the words ELSE IF together. The line should be ELSE IF light = "yellow" THEN.
Q4
B — The comparison points the wrong way. Speed 45 is less than 60, so it would say Too fast. The line should be IF speed > 60 THEN.
Q5
C — This should be ELSE IF. A plain IF starts a new check, so a dog would get the walk and also the cuddle. With ELSE IF only one path runs.