← IGCSE Computer Science Paper 2

Why use constants in a program

A named value used instead of a magic number. It makes code easier to read, and when the value changes you edit it in one place only, so no missed edits cause mistakes.

Understanding Why use constants in a program in IGCSE Computer Science Paper 2

What it does: The bare 0.2 is a magic number. Replacing it with a named constant such as VatRate would make the meaning clear. If the rate rises from 0.2 to 0.25, only this one line is edited. All lines that use VatRate then use the new value automatically. Constants make code more readable and easier to maintain. Their value is fixed while the program runs, so they are not changed by INPUT. The data type stays REAL. Using a named constant improves readability and makes updating simpler. Fix: declare CONSTANT VatRate ← 0.2 once and use VatRate on each line, so one edit updates them all.

Common mistakes: A constant is given its value with the ← operator, not =. It should be CONSTANT VatRate ← 0.2, so the named value is set in one place. 40 is a magic number here. The check should use the constant PassMark, so if the pass mark changes it only needs editing on line 1.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 317–326) and word search (Modules 317–336).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Why is CONSTANT VatRate ← 0.2 clearer than writing 0.2 in every calculation?
The name VatRate tells the reader what the value meansThe name VatRate makes the program run faster
Matches
Tax1 ← Price1 * 0.2 Tax2 ← Price2 * 0.2 Tax3 ← Price3 * 0.2
↓
The rate 0.2 is typed as a magic number on every line. When the rate changes, every line must be edited, which is slow and easy to get wrong.
Applying
Meanings
Term
CONSTANT VatRate ← 0.2
Meaning
A named value used instead of a magic number. It makes code easier to read, and when the value changes you edit it in one place only, so no missed edits cause mistakes.
Practice
CONSTANT VatRate ← 0.2
Testing
Gaps
VatRate ← 0.2
Check Answer
Spots
1DECLARE Price : REAL
2CONSTANT VatRate = 0.2

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Warthog' badge — Definition Digger, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Why is CONSTANT VatRate ← 0.2 clearer than writing 0.2 in every calculation?
A. The name VatRate tells the reader what the value means ✓
B. The name VatRate makes the program run faster
A named value explains itself. A bare 0.2 is a magic number, so a reader has to guess what it stands for.
2. The VAT rate changes from 0.2 to 0.25. The program uses the constant VatRate. How many places must you edit?
A. One: the CONSTANT line ✓
B. Every line that uses VatRate
The value is stored in one place, in the CONSTANT declaration. Every line that uses the name picks up the new value automatically.
3. Which line is better for a price calculation?
A. Total ← Price + Price * VatRate ✓
B. Total ← Price + Price * 0.2
The first line uses a named constant. The second uses a magic number, which is harder to read and harder to update.
4. A program has the number 0.2 typed in 15 different lines. The rate changes. What is the risk?
A. The program will refuse to start
B. You might miss one line, so some results use the old rate ✓
Missed edits lead to inconsistent results. A single constant removes this risk because there is only one value to change.
5. What is a magic number?
A. A number that always equals zero
B. A bare value in code with no name to explain its meaning ✓
A magic number is a literal such as 0.2 written directly in the code. Replacing it with a named constant makes the meaning clear.

Match the Pairs

CONSTANT VatRate ← 0.2 → Gives the number 0.2 a meaningful name, so later lines can use VatRate instead of an unexplained magic number
Total ← Price * VatRate → Uses the named constant in a calculation, so if the rate changes only the CONSTANT line needs editing
A magic number in a calculation is hard to understand, because the reader cannot tell what 0.2 stands for → Tax ← Price * 0.2
Changing a rate once: edit only the CONSTANT line and every calculation using the name gets the new value → CONSTANT VatRate ← 0.25
Tax1 ← Price1 * 0.2 Tax2 ← Price2 * 0.2 Tax3 ← Price3 * 0.2 → The rate 0.2 is typed as a magic number on every line. When the rate changes, every line must be edited, which is slow and easy to get wrong.
Tax1 ← Price1 * 0.25 Tax2 ← Price2 * 0.2 Tax3 ← Price3 * 0.25 → One line was missed when the rate was updated, so the program uses two different rates and gives wrong results.

Key Term

CONSTANT VatRate ← 0.2
A named value used instead of a magic number. It makes code easier to read, and when the value changes you edit it in one place only, so no missed edits cause mistakes.

Fill in the Blanks

1. CONSTANT VatRate ← 0.2 Tax ← Price * VatRate
Hint: Type the keyword that declares a named value that never changes while the program runs.
2. CONSTANT VatRate ← 0.2 Tax ← Price * VatRate
Hint: Type the symbol that gives the constant its value.
3. CONSTANT VatRate ← 0.2 Tax ← Price * VatRate OUTPUT Tax
Hint: Type the constant's name, which is the same name used in the calculation below.
4. CONSTANT VatRate ← 0.2 Tax ← Price * VatRate
Hint: Type the operator that multiplies the price by the named rate.
5. CONSTANT VatRate ← 0.25 Tax1 ← Price1 * VatRate Tax2 ← Price2 * VatRate
Hint: Type the constant's name instead of a magic number, so one edit updates both lines.

Spot the Error

Question 1
A DECLARE Price : REAL
B CONSTANT VatRate = 0.2
C INPUT Price
D OUTPUT Price * VatRate
Line B — A constant is given its value with the ← operator, not =. It should be CONSTANT VatRate ← 0.2, so the named value is set in one place.
Question 2
A CONSTANT PassMark ← 40
B IF Score >= 40
C THEN
D OUTPUT "Pass"
E ENDIF
Line B — 40 is a magic number here. The check should use the constant PassMark, so if the pass mark changes it only needs editing on line 1.
Question 3
A CONSTANT VatRate ← 0.2
B INPUT Price
C Total ← Price * (1 + 0.2)
D OUTPUT Total
Line C — The rate 0.2 is typed directly instead of using VatRate. If the VAT rate changes, this line could be missed. It should use VatRate.
Question 4
A DECLARE MaxScore ← 50
B INPUT Mark
C Pct ← Mark / MaxScore * 100
D OUTPUT Pct
Line A — MaxScore never changes, so it should be declared with CONSTANT, not DECLARE. A constant keeps the fixed value in one named place.
Question 5
A CONSTANT Rate ← 0.05
B INPUT Savings
C Interest ← Savings + Rate
D OUTPUT Interest
Line C — Interest is found by multiplying the savings by the rate, not adding. It should be Savings * Rate, using the constant Rate.

Student with a class code? Join your class →