Branching with IF ... THEN

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

Branching with IF ... THEN

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly starts a branch in pseudocode?
A IF it is raining THEN
B if it is raining then
2. The condition is false. What happens to the action inside IF ... THEN?
A The action is skipped
B The action still runs
3. Which one is set out correctly?
A IF score = 10 THEN play a sound END IF
B IF score = 10 THEN play a sound END IF
4. How do you finish an IF ... THEN branch?
A STOP
B END IF
5. Which part is the condition in this branch? IF door is open THEN walk through END IF
A walk through
B door is open

Match the Pairs

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

Left columnRight column
1IF it is raining THEN AIF, THEN and END IF must be written in capital letters so they stand out from the rest of the steps.
2END IF BChecks the condition 'it is raining'. The indented action below runs only if it is true.
3The action is indented so it is clearly part of the IF block and runs only when the condition is true. CIF door is open THEN walk through END IF say hello
4If the condition is false, the action is skipped and the program carries on after END IF. DThe action is not indented and there is no END IF to close the block.
5if battery is low then plug in charger end if EIF score > 10 THEN show "Well done!" END IF
6IF lights are off THEN turn on the lights FMarks the end of the IF block so the computer knows where the branching stops.

Definitions

IF condition THEN action END IF

Fill in the Blanks

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

1.
__ it is raining THEN take an umbrella END IF
2.
IF it is raining ____ take an umbrella END IF
3.
IF it is dark THEN turn on the lamp ______
4.
IF score _ 10 THEN SHOW "You win" END IF
5.
IF door _ locked THEN use the key END IF

Spot the Error

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

Question 1
A SET score TO 12
B IF score > 10
C SAY "Well done!"
D END IF
Question 2
A IF temp < 15 THEN
B SAY "Wear a jacket"
C END
Question 3
A SET bell TO "rang"
B if bell = "rang" then
C SAY "Time for recess"
D END IF
Question 4
A SET weather TO "hot"
B IF weather = "hot" THEN
C SAY "Wear a hat"
D END THEN
Question 5
A IF door is open
B walk through
C END IF

Answer Key

Branching with IF ... THEN
Section A: Quick Questions
Q1
A — IF it is raining THEN
Q2
A — The action is skipped
Q3
A — IF score = 10 THEN play a sound END IF
Q4
B — END IF
Q5
B — door is open
Section B: Match the Pairs
Q1
IF it is raining THEN → Checks the condition 'it is raining'. The indented action below runs only if it is true.
Q2
END IF → Marks the end of the IF block so the computer knows where the branching stops.
Q3
The action is indented so it is clearly part of the IF block and runs only when the condition is true. → IF score > 10 THEN show "Well done!" END IF
Q4
If the condition is false, the action is skipped and the program carries on after END IF. → IF door is open THEN walk through END IF say hello
Q5
if battery is low then plug in charger end if → IF, THEN and END IF must be written in capital letters so they stand out from the rest of the steps.
Q6
IF lights are off THEN turn on the lights → The action is not indented and there is no END IF to close the block.
Section C: Definitions
Q1
IF condition THEN action END IF — Runs the indented action only when the condition is true. If the condition is false, the action is skipped. Write IF and THEN in capitals, indent the action, and finish with END IF.
Section D: Fill in the Blanks
Q1
IF it is raining THEN take an umbrella END IF
Q2
IF it is raining THEN take an umbrella END IF
Q3
IF it is dark THEN turn on the lamp END IF
Q4
IF score > 10 THEN SHOW "You win" END IF
Q5
IF door = locked THEN use the key END IF
Section E: Spot the Error
Q1
B — The IF line is missing THEN. It should be 'IF score > 10 THEN' so the computer knows the action comes next.
Q2
C — An IF block must finish with END IF, not just END. The last line should be 'END IF'.
Q3
B — IF and THEN must be written in capitals. The line should be 'IF bell = "rang" THEN'.
Q4
D — The block must finish with END IF. THEN belongs at the end of the IF line, so line 4 should be 'END IF'.
Q5
A — The IF line is missing THEN. It should be 'IF door is open THEN' so the computer knows the action comes next.