A decision inside a decision

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

A decision inside a decision

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which describes a nested IF?
A Two IFs written one after the other with nothing inside them
B A second IF written inside the THEN or ELSE path of the first IF
2. In this algorithm, when is the inner IF checked? IF score > 10 THEN IF score > 50 THEN say "Wow!" END IF END IF
A Only when score > 10 is true
B Every time, even when score is 3
3. Which nested IF is laid out correctly?
A The inner IF is indented, and each IF has its own END IF
B The inner IF is not indented, and one END IF closes both IFs
4. A game has two IFs, one inside the other. How many END IF lines does it need?
A 2 END IF lines, one for each IF
B 1 END IF line, because the IFs are joined
5. Game level example: IF level = 1 THEN IF lives = 0 THEN say "Game over" END IF END IF If level is 2, what happens to the inner IF?
A It is checked and says Game over
B It is skipped and never checked

Match the Pairs

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

Left columnRight column
1IF score > 50 THEN IF lives > 0 THEN SAY "Next level" END IF END IF AThe inner IF is not indented, so it is hard to see that it is inside the first IF's THEN path.
2 IF lives > 0 THEN BIF level = 1 THEN SAY "Easy mode" ELSE IF level = 2 THEN SAY "Medium mode" END IF END IF
3Each IF needs its own END IF, so two IFs need two END IFs. CIF coins = 10 THEN IF key = 1 THEN SAY "Door opens" END IF END IF
4An IF inside the ELSE path is only checked when the first condition is false. DA nested IF: the inner IF about lives is only checked when the score is over 50.
5IF score > 50 THEN IF lives > 0 THEN SAY "Next level" END IF EThe outer IF has no END IF. Each IF needs its own END IF, so this needs a second END IF at the end.
6IF score > 50 THEN IF lives > 0 THEN SAY "Next level" END IF END IF FThe inner IF is indented to show it belongs inside the outer IF's path.

Definitions

nested IF

Fill in the Blanks

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

1.
IF score >= 50 THEN __ lives > 0 THEN SAY "Go to level 2"
2.
IF coins > 10 THEN IF key = "yes" ____ SAY "Door opens"
3.
SAY "Door opens" ______ END IF
4.
ELSE IF lives _ 0 THEN SAY "Try again"
5.
IF level _ 3 THEN IF boss_beaten = "yes" THEN SAY "You win!"

Spot the Error

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

Question 1
A IF lives > 0 THEN
B SET bonus TO 0
C IF score > 50 THEN
D SET bonus 10
E END IF
F END IF
Question 2
A IF key = 1 THEN
B IF door = open
C SAY You win
D END IF
E END IF
Question 3
A score >= 100 THEN
B IF lives > 0 THEN
C SET level TO 2
D END IF
E END IF
Question 4
A IF hasMap = YES THEN
B IF hasKey YES THEN
C SAY Open the chest
D END IF
E END IF
Question 5
A IF lives < 0 THEN
B IF score > 50 THEN
C SAY Bonus level
D END IF
E END IF

Answer Key

A decision inside a decision
Section A: Quick Questions
Q1
B — A second IF written inside the THEN or ELSE path of the first IF
Q2
A — Only when score > 10 is true
Q3
A — The inner IF is indented, and each IF has its own END IF
Q4
A — 2 END IF lines, one for each IF
Q5
B — It is skipped and never checked
Section B: Match the Pairs
Q1
IF score > 50 THEN IF lives > 0 THEN SAY "Next level" END IF END IF → A nested IF: the inner IF about lives is only checked when the score is over 50.
Q2
IF lives > 0 THEN → The inner IF is indented to show it belongs inside the outer IF's path.
Q3
Each IF needs its own END IF, so two IFs need two END IFs. → IF coins = 10 THEN IF key = 1 THEN SAY "Door opens" END IF END IF
Q4
An IF inside the ELSE path is only checked when the first condition is false. → IF level = 1 THEN SAY "Easy mode" ELSE IF level = 2 THEN SAY "Medium mode" END IF END IF
Q5
IF score > 50 THEN IF lives > 0 THEN SAY "Next level" END IF → The outer IF has no END IF. Each IF needs its own END IF, so this needs a second END IF at the end.
Q6
IF score > 50 THEN IF lives > 0 THEN SAY "Next level" END IF END IF → The inner IF is not indented, so it is hard to see that it is inside the first IF's THEN path.
Section C: Definitions
Q1
nested IF — A second IF written inside the THEN or ELSE path of the first IF. It is only checked when that path is taken. Indent it and give each IF its own END IF.
Section D: Fill in the Blanks
Q1
IF score >= 50 THEN IF lives > 0 THEN SAY "Go to level 2"
Q2
IF coins > 10 THEN IF key = "yes" THEN SAY "Door opens"
Q3
SAY "Door opens" END IF END IF
Q4
ELSE IF lives > 0 THEN SAY "Try again"
Q5
IF level = 3 THEN IF boss_beaten = "yes" THEN SAY "You win!"
Section E: Spot the Error
Q1
D — The step inside the inner IF is missing the word TO. It should be ' SET bonus TO 10'. This step only runs when both IFs are true.
Q2
B — The inner IF is missing THEN. Every IF needs THEN after its condition: ' IF door = open THEN'.
Q3
A — The outer decision must start with the word IF. It should be 'IF score >= 100 THEN'.
Q4
B — The inner condition is missing the = sign, so it does not compare anything. It should be ' IF hasKey = YES THEN'.
Q5
A — Lives are never below 0 while playing, so the THEN path is never taken and the inner IF is never checked. It should be 'IF lives > 0 THEN'.