Decisions inside loops in a visual program

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

Decisions inside loops in a visual program

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A Scratch script has a forever block with an if block inside it. How often is the if condition checked?
A Every time the loop repeats
B Only once, at the start
2. Which script keeps checking whether the player is touching the edge while the game runs?
A if touching edge? then ... (with no loop around it)
B forever { if touching edge? then ... }
3. Mia puts an if block BEFORE a forever block. The if checks 'key space pressed?'. What happens?
A It keeps checking the key for the whole game
B It only checks the key once, so it will probably miss later key presses
4. In 'repeat until score = 10', you add an if block inside that says 'if touching apple then change score by 1'. When is the if checked?
A On every repeat, until the score reaches 10
B Only on the very last repeat
5. What is the main reason to put an if block inside a loop?
A So the program can make a new decision each time round
B So the loop runs faster

Match the Pairs

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

Left columnRight column
1forever if touching edge? then turn 180 degrees Aforever if touching color red? then say Ouch!
2repeat until score = 10 if key space pressed? then change score by 1 Brepeat until lives = 0 if touching enemy? then change lives by -1
3An if block inside a loop is checked again on every repeat, so the program can make a new decision each time. CThe loop keeps checking for the space key and adds 1 to score each time it is pressed, until score reaches 10.
4A repeat until loop can hold an if block, so the loop checks its own stop condition and also makes decisions inside. DThe second if block sits below the loop, so the bomb is only checked once after the loop ends, not on every repeat.
5if touching edge? then forever turn 15 degrees EThe sprite checks the edge on every repeat, forever, and turns whenever it is touching the edge.
6repeat until score = 5 if touching star? then change score by 1 if touching bomb? then say Boom! FThe if block is outside the loop, so touching edge? is checked only once, at the start, and never again.

Definitions

if inside a loop

Fill in the Blanks

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

1.
_______ if <key space pressed?> then say "Jump!"
2.
forever __ <touching edge?> then turn 15 degrees
3.
repeat until <score = 10> __ <touching coin?> then change score by 1
4.
repeat _____ <score = 10> if <touching coin?> then change score by 1
5.
repeat until <lives = 0> if <touching ghost?> then change _____ by -1

Spot the Error

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

Question 1
A repeat 1
B if touching edge? then
C turn 15 degrees
D move 10 steps
Question 2
A set score to 0
B repeat until score = 5
C if touching star? then
D set score to 1
E say You win!
Question 3
A when green flag clicked
B forever
C when touching Cat? then
D say Caught you!
Question 4
A set lives to 3
B repeat until lives > 0
C if touching Ghost? then
D change lives by -1
Question 5
A set speed to 0
B forever
C if speed < 10 then
D say Too fast!
E change speed by 1

Answer Key

Decisions inside loops in a visual program
Section A: Quick Questions
Q1
A — Every time the loop repeats
Q2
B — forever { if touching edge? then ... }
Q3
B — It only checks the key once, so it will probably miss later key presses
Q4
A — On every repeat, until the score reaches 10
Q5
A — So the program can make a new decision each time round
Section B: Match the Pairs
Q1
forever if touching edge? then turn 180 degrees → The sprite checks the edge on every repeat, forever, and turns whenever it is touching the edge.
Q2
repeat until score = 10 if key space pressed? then change score by 1 → The loop keeps checking for the space key and adds 1 to score each time it is pressed, until score reaches 10.
Q3
An if block inside a loop is checked again on every repeat, so the program can make a new decision each time. → forever if touching color red? then say Ouch!
Q4
A repeat until loop can hold an if block, so the loop checks its own stop condition and also makes decisions inside. → repeat until lives = 0 if touching enemy? then change lives by -1
Q5
if touching edge? then forever turn 15 degrees → The if block is outside the loop, so touching edge? is checked only once, at the start, and never again.
Q6
repeat until score = 5 if touching star? then change score by 1 if touching bomb? then say Boom! → The second if block sits below the loop, so the bomb is only checked once after the loop ends, not on every repeat.
Section C: Definitions
Q1
if inside a loop — An if block placed inside a loop is checked again on every repeat, so the program makes a new decision each time round, like in forever or repeat until.
Section D: Fill in the Blanks
Q1
forever if <key space pressed?> then say "Jump!"
Q2
forever if <touching edge?> then turn 15 degrees
Q3
repeat until <score = 10> if <touching coin?> then change score by 1
Q4
repeat until <score = 10> if <touching coin?> then change score by 1
Q5
repeat until <lives = 0> if <touching ghost?> then change lives by -1
Section E: Spot the Error
Q1
A — The if block must sit inside a forever loop so it is checked on every repeat. With repeat 1, the loop runs only once, so the sprite checks for the edge just one time. Line 1 should be forever.
Q2
D — Each time the if block is true, the score should go up by 1. Setting it to 1 every time means the score never reaches 5, so the loop never ends. Line 4 should be change score by 1.
Q3
C — Inside the forever loop we need an if block to make a decision on every repeat. 'when' is not a decision block. Line 3 should be if touching Cat? then.
Q4
B — Lives starts at 3, so lives > 0 is true straight away and the loop stops before the if block is checked even once. It should be repeat until lives = 0.
Q5
C — The sprite should only say 'Too fast!' when the speed is greater than 10. Using < makes it say that while it is still slow. Line 3 should be if speed > 10 then.