GoalPredict what a program with an if … then block does when the condition is true and when it is false.
New words
outcome · what a program does when it runspredict · to say what will happen before the program runstrue · the condition is correct at the moment that it is checkedfalse · the condition is not correct at the moment that it is checked
Explanation
A program with an if … then block has two possible outcomes. If the condition is true, the conclusion is carried out. If the condition is false, the conclusion is skipped. To predict the outcome, work through the blocks one at a time, just as the computer does. Each time that the if block is reached, check its condition. A forever block makes the program come back to the if block again and again. Write down what happens at each pass. Then run the program and compare the result with your prediction.
Worked examples
Press
Penguin’s x after moving
Touching the Fish?
Outcome
1
-50
no
nothing
2
0
no
nothing
3
50
no
nothing
4
100
yes
says Lunch!
The Fish stays at x: 100, y: 0. The sprites are small, so they touch only when they are at the same spot. Each press of the right arrow key moves the Penguin 50 steps. The Penguin says ‘Lunch!’ only on press 4.
A forever loop with a wait block checks the condition once every second. The sprite moves up only at the checks where the key is held down.
Second
1
2
3
4
5
6
Space key held?
yes
yes
yes
no
yes
no
y after the check
10
20
30
30
40
40
The sprite starts at y: 0. The key is held at four of the six checks, so the sprite moves up 4 times and ends at y: 40.
Class activity
Track game (pairs). Draw a track of 10 squares in a row and number them 1 to 10. Put a counter on square 1. Rule A: each ‘press’ moves the counter 3 squares forward. Rule B: if the counter is on square 7, then say ‘Treat!’. Before you play, predict on which press the counter says ‘Treat!’. Play it and check. Now change rule A to 2 squares, and then to 4 squares. Predict first each time. What happens with 4 squares, and why?
Practice
1
The Goat starts at x: -90 and moves 30 steps for each press of the up arrow key. The Gate is at x: 90, and they touch only on the same spot. After which press does the Goat say ‘Baa!’?
After press 6. The Goat is at -60, -30, 0, 30, 60 and then 90 after presses 1 to 6.
2
The Sheep starts at x: -90 and moves 40 steps for each press. The Gate is at x: 100. Will the Sheep ever say ‘Baa!’? Explain.
Press
1
2
3
4
5
Sheep’s x
?
?
?
?
?
No. The Sheep is at -50, -10, 30, 70, 110 and so on. It jumps over x: 100 and is never at the same spot as the Gate, so the condition is never true.
3
A forever block checks every second. The key is held at checks 1, 3 and 4 and not held at checks 2 and 5. Each true check changes x by 15. The sprite starts at x: 0. Where is it after the 5 checks?
Check
1
2
3
4
5
Key held?
yes
no
yes
yes
no
It moves 3 times: 3 × 15 = 45. The sprite is at x: 45.
4
Write the two outcomes of this program.
If the Ball touches the Net, the pop sound plays. If it does not touch the Net, nothing plays and the program ends, because the if block is checked only once.