Branching with IF ... THEN ... ELSE

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

Branching with IF ... THEN ... ELSE

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. IF it is raining THEN take an umbrella ELSE wear a hat. It is raining. What happens?
A Take an umbrella
B Wear a hat
2. IF the light is green THEN go ELSE stop. The light is red. What happens?
A Go
B Stop
3. In IF ... THEN ... ELSE, how many of the two paths run each time?
A Both paths
B Exactly one path
4. When does the ELSE path run?
A When the condition is false
B When the condition is true
5. Which algorithm gives a choice between two outcomes?
A IF score > 10 THEN say 'Win' ELSE say 'Try again'
B IF score > 10 THEN say 'Win'

Match the Pairs

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

Left columnRight column
1IF raining THEN take umbrella ELSE wear sunhat AIF score >= 10 THEN give prize ELSE try again
2ELSE wear sunhat BBoth paths do the same action, so the ELSE path does not give a different outcome. The ELSE action should be something else, like wear a t-shirt.
3Two outcomes: if the score is 10 or more you get a prize, otherwise you try again CThe path that runs when the condition is false
4Following each path: if the light is green you go, but if it is not green you stop DThe ELSE has no action after it. It needs a second action, like push the door open.
5IF it is cold THEN wear a jumper ELSE wear a jumper EChooses between two paths: take the umbrella if it is raining, otherwise wear the sunhat. Only one happens.
6IF door is locked THEN use key ELSE FIF light is green THEN go ELSE stop

Definitions

IF condition THEN action1 ELSE action2

Fill in the Blanks

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

1.
__ door is open THEN close it ELSE open it
2.
IF it is raining THEN take an umbrella ____ wear a cap
3.
IF score > 10 ____ show "You win!" ELSE show "Try again"
4.
IF temperature _ 25 THEN wear shorts ELSE wear jeans
5.
IF the light is green THEN go ELSE ____

Spot the Error

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

Question 1
A ASK for the temperature
B IF temperature < 15 THEN
C SAY wear a jumper
D ELSE temperature >= 15
E SAY wear a t-shirt
F END IF
Question 2
A IF score > 10 THEN
B SAY You win
C OTHERWISE
D SAY Try again
E END IF
Question 3
A SET age TO 9
B IF age >= 12
C SAY Teen ticket
D ELSE
E SAY Child ticket
F END IF
Question 4
A IF the door is open THEN
B walk inside
C ELSE
D walk inside
E END IF
Question 5
A SET coins TO 3
B IF coins < 5 THEN
C buy the game
D ELSE
E keep saving
F END IF

Answer Key

Branching with IF ... THEN ... ELSE
Section A: Quick Questions
Q1
A — Take an umbrella
Q2
B — Stop
Q3
B — Exactly one path
Q4
A — When the condition is false
Q5
A — IF score > 10 THEN say 'Win' ELSE say 'Try again'
Section B: Match the Pairs
Q1
IF raining THEN take umbrella ELSE wear sunhat → Chooses between two paths: take the umbrella if it is raining, otherwise wear the sunhat. Only one happens.
Q2
ELSE wear sunhat → The path that runs when the condition is false
Q3
Two outcomes: if the score is 10 or more you get a prize, otherwise you try again → IF score >= 10 THEN give prize ELSE try again
Q4
Following each path: if the light is green you go, but if it is not green you stop → IF light is green THEN go ELSE stop
Q5
IF it is cold THEN wear a jumper ELSE wear a jumper → Both paths do the same action, so the ELSE path does not give a different outcome. The ELSE action should be something else, like wear a t-shirt.
Q6
IF door is locked THEN use key ELSE → The ELSE has no action after it. It needs a second action, like push the door open.
Section C: Definitions
Q1
IF condition THEN action1 ELSE action2 — Chooses between two paths. If the condition is true, action1 runs. If it is false, the ELSE path runs action2 instead. Exactly one of the two paths runs.
Section D: Fill in the Blanks
Q1
IF door is open THEN close it ELSE open it
Q2
IF it is raining THEN take an umbrella ELSE wear a cap
Q3
IF score > 10 THEN show "You win!" ELSE show "Try again"
Q4
IF temperature > 25 THEN wear shorts ELSE wear jeans
Q5
IF the light is green THEN go ELSE stop
Section E: Spot the Error
Q1
D — ELSE never has its own condition. It just means 'otherwise'. Line 4 should be just ELSE.
Q2
C — The keyword for the second path is ELSE, not OTHERWISE. Line 3 should be ELSE.
Q3
B — The IF line is missing THEN after the condition. It should be IF age >= 12 THEN.
Q4
D — Both paths now do the same thing, so there is no real choice. The ELSE path should do something different, like knock on the door.
Q5
B — The condition is the wrong way around. With 3 coins the program would buy the game. It should be IF coins >= 5 THEN.