GoalReview variables, data types and Scratch blocks by tracing programs.
New words
variable · a named place in memory where a program keeps data that can changedata type · the kind of data, such as integer, string or charactercondition · a question that is either true or falseloop · a group of blocks that repeats
Explanation
A variable has a name and a value, and the value can change. The set block gives a variable a starting value, and the change block adds a number to it, or takes a number away if the number is negative. A new variable in Scratch starts at 0, so we usually set it at the start. Whole numbers are integers, text is a string and a single letter, digit or symbol is a character. A repeat loop runs a fixed number of times. A repeat until loop tests its condition before each turn and stops as soon as it is true. An if block runs its blocks only when its condition is true. To trace a program, write the value of each variable after each block.
Data
Data type
12
integer
‘Room 4’
string
‘Q’
character
‘@’
character
Worked examples
Trace this program. The variable coins starts at 5. The loop runs 3 times and adds 2 each time, so coins goes 7, 9 and 11. The condition ‘coins more than 10’ is true for 11, so the sprite says Rich!
This loop tests its condition before each turn. The variable lamps goes 10, 7, 4 and then 1. At 10, 7 and 4 the condition ‘lamps less than 3’ is false, so the loop runs. At 1 it is true, so the loop stops.
Turn
lamps after the change
start
10
1
7
2
4
3
1
A trace table records the values after each turn of the loop.
Class activity
Be the computer (no computer): write the variable name coins on a card and put a number on it. Follow the coins program line by line. Each time you reach a change block, cross out the number and write the new one. Say what the sprite says at the end. Then change the starting number to 0 and the number added to 4, and trace it again. Does the sprite say Rich! this time? If you have a computer or tablet, build the program in Scratch and compare the result with your trace.
Practice
1
Trace the program. What does the sprite say, and what is the value of score at the end?
score is 20 − 5 = 15. Since 15 is less than 20 the condition is true, so the sprite says Lower.
2
Give the best data type for each: (a) the number of pages in a book (b) the name of a pet (c) the first letter of your name (d) a bus stop called ‘Park Road 2’.
(a) integer. (b) string. (c) character. (d) string, because it has letters, a space and a digit.
3
A program sets n to 1 and then repeats until n is more than 20, changing n by 6 each time. List the values of n and say what it is when the loop stops.
n goes 1, 7, 13, 19, 25. At 25 the condition is true, so the loop stops. The final value is 25.
4
Plan a game about catching raindrops. Name two variables, give a data type for each, say what happens when the green flag is clicked and what ends the game.
For example: drops (integer) and playerName (string). When the green flag is clicked, drops is set to 0. Each caught drop changes drops by 1. The game ends when drops reaches 10, using a condition that tests drops.