Variables in an algorithm

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

Variables in an algorithm

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does SET score TO 0 do in an algorithm?
A It prints the word score on the screen
B It stores the value 0 in a variable called score
2. Which one is a variable name?
A lives
B TO
3. Which step makes a counter go up by one?
A ADD 1 to score
B SET score TO score
4. score is 0. After SET score TO 5, what is score?
A 5
B 0
5. A variable can only ever hold one value and can never change.
A True: it is fixed once it is made
B False: its value can be set and changed

Match the Pairs

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

Left columnRight column
1SET score TO 0 AThe variable name and value are swapped. The name score should come after SET and the value 0 after TO: SET score TO 0.
2ADD 1 TO score BThe steps are in the wrong order. The variable is changed before it has a starting value, so the counter would be reset to 0 and the count lost. SET should come first.
3A counter variable starts at zero before counting begins CSET lives TO 3 ADD 1 TO lives
4A variable can store a value that changes later in the algorithm DSET lapCount TO 0
5ADD 1 TO score SET score TO 0 EMakes a variable called score and gives it the starting value 0
6SET 0 TO score FChanges the variable score by making its stored value one bigger

Definitions

SET score TO 0

Fill in the Blanks

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

1.
___ score TO 0
2.
SET score __ 0
3.
SET score TO _
4.
SET score TO 0 ADD 1 TO _____ SHOW score
5.
SET count TO 0 REPEAT 3 TIMES ___ 1 TO count

Spot the Error

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

Question 1
A SET score TO 0
B REPEAT 3 TIMES
C ADD 1 TO points
D DISPLAY score
Question 2
A SET lives TO 3
B SET score TO 0
C ADD 1 TO score
D DISPLAY scores
Question 3
A SET steps TO 0
B ADD 1 TO steps
C ADD steps TO 1
D DISPLAY steps
Question 4
A SET count TO 0
B ADD 1 count
C ADD 1 TO count
D DISPLAY count
Question 5
A SET lives TO 3
B SUBTRACT 1 FROM lives
C SUBTRACT 1 FROM lives
D DISPLAY 3

Answer Key

Variables in an algorithm
Section A: Quick Questions
Q1
B — It stores the value 0 in a variable called score
Q2
A — lives
Q3
A — ADD 1 to score
Q4
A — 5
Q5
B — False: its value can be set and changed
Section B: Match the Pairs
Q1
SET score TO 0 → Makes a variable called score and gives it the starting value 0
Q2
ADD 1 TO score → Changes the variable score by making its stored value one bigger
Q3
A counter variable starts at zero before counting begins → SET lapCount TO 0
Q4
A variable can store a value that changes later in the algorithm → SET lives TO 3 ADD 1 TO lives
Q5
ADD 1 TO score SET score TO 0 → The steps are in the wrong order. The variable is changed before it has a starting value, so the counter would be reset to 0 and the count lost. SET should come first.
Q6
SET 0 TO score → The variable name and value are swapped. The name score should come after SET and the value 0 after TO: SET score TO 0.
Section C: Definitions
Q1
SET score TO 0 — A variable is a named place that stores a value. SET gives it a value, like 0, and later steps can change it, such as ADD 1 to score, so it works as a counter.
Section D: Fill in the Blanks
Q1
SET score TO 0
Q2
SET score TO 0
Q3
SET score TO 0
Q4
SET score TO 0 ADD 1 TO score SHOW score
Q5
SET count TO 0 REPEAT 3 TIMES ADD 1 TO count
Section E: Spot the Error
Q1
C — The variable was named score, but this line adds to points, which was never set. It should be ADD 1 TO score.
Q2
D — There is no variable called scores. The variable is named score, so the line should be DISPLAY score.
Q3
C — The words are in the wrong order. The number goes first and the variable goes last, so it should be ADD 1 TO steps.
Q4
B — The word TO is missing. The step should be ADD 1 TO count so it is clear which variable gets the 1 added.
Q5
D — This shows the number 3 instead of the variable. Lives has changed to 1, so the line should be DISPLAY lives.