GoalReview variables and constants, outline plans, input, process and output, and debugging.
New words
variable · a named place that stores a value that can changeconstant · a value that does not change while the program runsoutline plan · a short plan with the purpose, the events and the outcomesdebug · to find a bug and fix it
Explanation
A variable stores a value that can change, and the block ‘set’ gives it a new value. The block ‘change’ adds to the old value. A constant is a value that never changes while the program runs. Before we write a program, we make an outline plan. It says the purpose, the events and the outcomes. Many programs have three parts: input, process and output. If a program gives a wrong result, we debug it. We compare the code with the algorithm and trace it. We look for a wrong order, a wrong number, a wrong name or a wrong sprite.
plan→write→trace→debug
Worked examples
Trace the program. The variable coins starts at 10. After ‘change by −3’ it is 7. After ‘change by 5’ it is 12. The sprite says 12.
Part
Plant game
purpose
count the waterings and say when the plant has grown
events
flag: set waterings to 0. Key w: change waterings by 1
outcomes
if waterings = 4, the plant says ‘I have grown!’
This outline plan is for a plant game. The purpose is to count how often the plant is watered. There are two events, and the outcome happens when the count is exactly 4.
The algorithm says: set total to 0, add 5, add 5, say total. The code should say 10, but it says 5. The second ‘set’ replaces the value instead of adding to it. Use ‘change’ instead.
Class activity
Variable jars (3 players, three cups and slips of paper). A cup is a variable and the number on the slip is its value. Player 1 reads cards such as ‘set jam to 6’, ‘change jam by 2’, ‘change jam by −5’. Player 2 is the computer and changes the slip in the cup. Player 3 checks with a pencil on paper. After each card, say the value aloud. Then write a card of your own for the other team that ends with the value 9.
Practice
1
Trace the program. What does the sprite say?
lives starts at 3. After −1 it is 2. After another −1 it is 1. After +2 it is 3. The sprite says 3.
2
Say variable or constant: (a) the score, (b) the number of lives at the start of every game, (c) the seconds left, (d) the value of one coin, which is always 2.