Choosing a variable or a constant

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

Choosing a variable or a constant

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which should store the VAT rate of 0.2 that never changes while the program runs?
A A variable: DECLARE VATRate : REAL
B A constant: CONSTANT VATRate ← 0.2
2. Which should store pi (3.14159) used to calculate the area of circles?
A A constant, because pi never changes
B A variable, because it is a REAL number
3. A quiz program adds 1 to the player's score after each right answer. What should score be?
A A constant, because it starts at 0
B A variable, because its value changes
4. The user types their answer to a question using INPUT. What should store it?
A A variable: DECLARE Answer : STRING
B A constant: CONSTANT Answer ← ""
5. Which statement is a correct reason for using a constant?
A The value is fixed while the program runs
B The value will be typed in by the user

Match the Pairs

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

Left columnRight column
1CONSTANT VATRate ← 0.2 ACONSTANT Pi ← 3.142
2DECLARE Score : INTEGER BA fixed value that is set once and never changes while the program runs
3Pi never changes while the program runs, so store it as a constant CThe VAT rate is fixed for the whole program, so it should be a constant (CONSTANT VATRate ← 0.2), not a variable that could be changed.
4The user's answer is typed in and can be different each time, so store it in a variable DDECLARE Answer : STRING INPUT Answer
5DECLARE VATRate : REAL VATRate ← 0.2 EA variable for a value that can change as the program runs, such as a player's score
6CONSTANT Score ← 0 Score ← Score + 10 FA score changes as the program runs, so it must be a variable (DECLARE Score : INTEGER), because a constant cannot be updated.

Definitions

CONSTANT

Fill in the Blanks

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

1.
________ VATRate ← 0.2
2.
_______ Score : INTEGER
3.
CONSTANT __ ← 3.14
4.
DECLARE Answer : STRING INPUT ______
5.
CONSTANT VATRate ← 0.2 Total ← Price * (1 + _______)

Spot the Error

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

Question 1
A CONSTANT VATRate ← 0.2
B CONSTANT Price ← 0.0
C INPUT Price
D OUTPUT Price * (1 + VATRate)
Question 2
A DECLARE Price : REAL
B DECLARE VATRate : REAL
C INPUT Price
D OUTPUT Price * (1 + VATRate)
Question 3
A CONSTANT MaxLives ← 3
B DECLARE Lives : INTEGER
C MaxLives ← Lives
D Lives ← Lives - 1
Question 4
A DECLARE Answer : STRING
B CONSTANT Answer ← "yes"
C OUTPUT "You typed ", Answer
Question 5
A DECLARE Score : INTEGER
B Score ← 0
C CONSTANT Score ← Score + 1
D OUTPUT Score

Answer Key

Choosing a variable or a constant
Section A: Quick Questions
Q1
B — A constant: CONSTANT VATRate ← 0.2
Q2
A — A constant, because pi never changes
Q3
B — A variable, because its value changes
Q4
A — A variable: DECLARE Answer : STRING
Q5
A — The value is fixed while the program runs
Section B: Match the Pairs
Q1
CONSTANT VATRate ← 0.2 → A fixed value that is set once and never changes while the program runs
Q2
DECLARE Score : INTEGER → A variable for a value that can change as the program runs, such as a player's score
Q3
Pi never changes while the program runs, so store it as a constant → CONSTANT Pi ← 3.142
Q4
The user's answer is typed in and can be different each time, so store it in a variable → DECLARE Answer : STRING INPUT Answer
Q5
DECLARE VATRate : REAL VATRate ← 0.2 → The VAT rate is fixed for the whole program, so it should be a constant (CONSTANT VATRate ← 0.2), not a variable that could be changed.
Q6
CONSTANT Score ← 0 Score ← Score + 10 → A score changes as the program runs, so it must be a variable (DECLARE Score : INTEGER), because a constant cannot be updated.
Section C: Definitions
Q1
CONSTANT — Use CONSTANT for a value fixed while the program runs, e.g. VAT rate or pi. Use a variable (DECLARE) for a value that changes or is input, e.g. score or answer.
Section D: Fill in the Blanks
Q1
CONSTANT VATRate ← 0.2
Q2
DECLARE Score : INTEGER
Q3
CONSTANT Pi ← 3.14
Q4
DECLARE Answer : STRING INPUT Answer
Q5
CONSTANT VATRate ← 0.2 Total ← Price * (1 + VATRate)
Section E: Spot the Error
Q1
B — Price is entered by the user, so its value changes. It must be a variable, DECLARE Price : REAL, not a CONSTANT.
Q2
B — The VAT rate is fixed while the program runs, so it should be a constant: CONSTANT VATRate ← 0.2.
Q3
C — MaxLives is a constant and cannot be assigned a new value. The changing value must go into the variable: Lives ← MaxLives.
Q4
B — The answer is typed in by the user and changes each time. It needs a variable and INPUT Answer, not a CONSTANT with a fixed value.
Q5
C — A score changes as the program runs, so it is a variable. It is updated with an assignment: Score ← Score + 1. CONSTANT cannot be used.