Teachers' Portal  /  IGCSE Computer Science 0478  /  Programming  /  Variables, constants and data types
Lesson pack · IGCSE Computer Science 0478 · 8.1 · IGCSE

Variables, constants and data types

Name it, type it, store it. Every program stores data while it runs: a score, a price, a name.

Student lesson Lab, full screen
At a glance

The shape of the lesson

Topic
Programming
Syllabus reference
Cambridge IGCSE Computer Science 0478, 8.1
Level
IGCSE
Duration
45 minutes, lab-led; stretches to a 60 minute block (see the contingency notes)
Position in topic
Lesson 1 of 11 in Programming; leads into Sequence and selection: IF and CASE
Central model
Fig. 1: each variable has an identifier, a value and a data type
Cooperative structure
Assessment
Stage 1 check and the exam-style plenary from the student lesson, plus the worksheet as the exit task
Learning objectives

By the end of the lesson, learners can

Section by section
  • Variables and constants: Run the totalling program in the lab one line at a time.
  • Five data types: Choose the data type from what the data is and what the program will do with it.
  • Input, output and sequence: INPUT stores a value typed by the user in a variable; OUTPUT displays values.
Key points to leave on the board
  • Variable: named memory location; its value can change while the program runs.
  • Constant: named value that cannot change while the program runs; declared with CONSTANT and a literal.
  • Data types: INTEGER, REAL, CHAR, STRING, BOOLEAN. Phone numbers are STRING.
  • Sequence: work through statements in order, updating each variable as you go.
The core visual model

One picture every idea returns to

A variable is a named memory location that stores a value which can change while the program runs. A constant is a named value that cannot change while the program runs. In pseudocode you declare them with DECLARE Score : INTEGER and CONSTANT VatRate ← 0.2, and assign with the arrow ←. The five basic data types are INTEGER, REAL, CHAR, STRING and BOOLEAN. Statements run one after another in sequence, so their order matters.

five variables, five basic data types ScorePriceGradeNamePassed 124.99'A' "Ali"TRUE INTEGERREALCHAR STRINGBOOLEAN whole numberhas decimalsone character textTRUE or FALSE a variable's value can change while the program runs; a constant's value cannot
Fig. 1: each variable has an identifier, a value and a data type
Lesson sequence

Forty-five minutes, phase by phase

TimePhaseWhat happens in the roomGrouping
0 to 5 minHook: predictOpen with the question in the title, Name it, type it, store it.. Every program stores data while it runs: a score, a price, a name. Put the figure on the board: it is the one picture every idea in the lesson returns to.Whole class, then pairs
5 to 13 minTeach and check: Variables and constantsRun the totalling program in the lab one line at a time. Project tab 1 of the lab (Sequence, selection and iteration with a trace table) and let a student drive it while the class predicts.Whole class, then pairs
13 to 21 minTeach and check: Five data typesChoose the data type from what the data is and what the program will do with it. Model the worked example Choosing data types on the board, one line at a time. Your turn: Which data type should store the price of a cinema ticket, 7.50?Pairs
21 to 30 minTeach and check: Input, output and sequenceINPUT stores a value typed by the user in a variable; OUTPUT displays values. Model the worked example Dry run on the board, one line at a time. Your turn: What is output by A ← 3, A ← A * 4, B ← A - 2, OUTPUT B?Think-Pair-Share
30 to 36 minCheckThe four Stage 1 questions from the student lesson, answered on mini whiteboards or by show of fingers. Re-teach the one most get wrong.Individual, then whole class
36 to 43 minPlenaryThe exam-style questions from Stage 2, one at a time: thirty seconds alone, then a random call. The feedback lines in the key give the examiner's wording.Individual, random call
43 to 45 minExit and homeworkSet the worksheet below (6 questions, 19 marks). Learners self-assess against the objectives before they leave.Individual
Timing and contingency

Protect the parts that carry the learning

The sequence carries about fifty minutes of material, so the lab is driven by one student while the class predicts rather than explored freely. Times are a guide.

Protected: the check at 30 minutes. It is the only individual read on whether the key idea landed, and it is not cut.

Cut first if the build overruns: the last check in Input, output and sequence, never the check or the plenary.

In a 60 minute block: run worksheet questions 1 to 3 in class as a Rally Coach set and model both the hardest worked example and its check live.

Running the cooperative task

Think-Pair-Share

Pose the question, give thirty seconds of silent thinking, then a minute to compare with a partner before a few pairs share with the class. The silent minute is the part that cannot be cut.

The full structure, with its variations and the other twelve, is on the cooperative-learning page.

Examiner traps to pre-empt

What to head off, and how

Trap learners fall intoTeaching move that pre-empts it
"A constant never changes" is not enough on its own.Say that its value cannot be changed while the program is running, and give a benefit: it only has to be updated in one place.
A phone number is a STRING, not an INTEGER: storing it as a number would lose the leading zero, and nobody adds or multiplies phone numbers.Return to Five data types: ask a pair to state the correct version and show it on the lab before moving on.
OUTPUT "Total" displays the word Total; OUTPUT Total displays the value stored in the variable Total.Check your quotation marks.
Worksheet

Variables, constants and data types: worksheet

6 questions, 19 marks. Written in the style of the papers; no past paper question is reproduced.

NameClassDate
Q1 · explain[2 marks]

Explain the difference between a variable and a constant.

Q2 · apply[5 marks]

A school stores these items for each student. Give the most suitable data type for each: (a) StudentName (b) Age (c) AverageMark, e.g. 67.5 (d) HasPaidTrip (e) FormLetter, e.g. 'C'.

Q3 · write[4 marks]

Write pseudocode that declares a constant Pi with the value 3.142, inputs the radius of a circle, then calculates and outputs the area (Pi × radius2).

Q4 · trace[3 marks]

State the output of this pseudocode.

A ← 4
B ← 3
C ← A * B - 2
A ← C + A
B ← A - B
OUTPUT A, " ", B, " ", C
Q5 · explain[2 marks]

A programmer stores mobile phone numbers as INTEGER. Explain why STRING would be a better choice.

Q6 · analysis[3 marks]

A student chooses the identifiers 2ndScore, Total Mark and OUTPUT. Explain why each one is not allowed.

Answer key and marking notes
Q1 · 2 marks
  • A variable is a named memory location whose value can change while the program is running.
  • A constant is a named value that cannot be changed while the program is running.
Q2 · 5 marks
  • (a) STRING
  • (b) INTEGER
  • (c) REAL
  • (d) BOOLEAN
  • (e) CHAR
Q3 · 4 marks
  • CONSTANT Pi ← 3.142
  • DECLARE Radius : REAL and DECLARE Area : REAL
  • INPUT Radius
  • Area ← Pi * Radius ^ 2 then OUTPUT Area (or OUTPUT with a message)
Q4 · 3 marks
  • C ← 4 * 3 - 2 = 10.
  • A ← 10 + 4 = 14; B ← 14 - 3 = 11.
  • Output: 14 11 10
Q5 · 2 marks
  • A number stored as an integer would lose its leading zero (and could not hold spaces or a + sign).
  • No arithmetic is ever done on a phone number, so it does not need a numeric type.
Q6 · 3 marks
  • 2ndScore starts with a digit; an identifier must start with a letter.
  • Total Mark contains a space; identifiers contain only letters and digits.
  • OUTPUT is a keyword, and keywords must never be used as identifiers.
Quiz

6 multiple-choice questions on this lesson

From the topic question bank. Assign them in the app for a marked score in your gradebook, or run the topic as a class game.

Q11 mark

Which statement best describes a constant in a program?

  • AA named value that cannot be changed while the program is running
  • BA named memory location whose value can change while the program is running
  • CA value that the user types in each time the program is run
  • DA variable that must be declared on the first line of the program
Q21 mark

A program stores a customer's telephone number, for example 07700 900123. Which data type is most suitable?

  • ASTRING
  • BINTEGER
  • CREAL
  • DCHAR
Q31 mark

A club records, for each member, whether they have paid this year's fee. Which declaration is most suitable?

  • ADECLARE HasPaid : BOOLEAN
  • BDECLARE HasPaid : CHAR
  • CDECLARE HasPaid : STRING
  • DCONSTANT HasPaid ← TRUE
Q41 mark

What is output by this pseudocode?

A ← 4
B ← A + 3
A ← B * 2
B ← A - B
OUTPUT A, " ", B

  • A14 7
  • B7 14
  • C8 4
  • D14 -7
Q51 mark

X and Y are INTEGER variables holding different values. Which sequence of statements correctly swaps their values, using an extra variable Temp?

  • ATemp ← X then X ← Y then Y ← Temp
  • BX ← Y then Y ← X then Temp ← X
  • CTemp ← X then Y ← Temp then X ← Y
  • DTemp ← Y then X ← Y then Y ← Temp
Q61 mark

This program is meant to add VAT to a price.

CONSTANT VatRate ← 0.2
DECLARE Price : REAL
DECLARE Total : REAL
INPUT Price
VatRate ← 0.25
Total ← Price + Price * VatRate
OUTPUT "Total to pay: ", Total
Why will this program not be accepted?

  • AIt tries to assign a new value to VatRate, which is a constant
  • BPrice must be declared as an INTEGER because it is input
  • CTotal must be given a value before it is used in the calculation
  • DThe OUTPUT statement cannot mix text and a variable
Quiz key
Q1: A

A constant is given its value once, in the code, and that value cannot be changed while the program runs (it only has to be edited in one place if it ever needs updating). A named location whose value can change is a variable. A value typed in by the user is input, which is stored in a variable. Where a declaration appears does not make something a constant.

Q2: A

A telephone number is a STRING: it can start with 0 and contain a space, and nobody adds or multiplies phone numbers. As an INTEGER the leading zero would be lost and the space is not allowed. REAL is for numbers with a fractional part. CHAR holds only a single character.

Q3: A

Paid or not paid has exactly two states, so BOOLEAN (TRUE or FALSE) is the right type for a variable. CHAR or STRING could store 'Y' or "Yes" but allow meaningless values and are less suitable. A constant cannot be used because the value differs from member to member and changes when a member pays.

Q4: A

B becomes 4 + 3 = 7, then A becomes 7 * 2 = 14, then B becomes 14 - 7 = 7, so the output is 14 7. 7 14 prints the values the wrong way round. 8 4 ignores the order of the lines (using A = 4 when calculating A ← B * 2 as A * 2). 14 -7 works out B - A instead of A - B on the fourth line.

Q5: A

Saving X in Temp first means its value survives when X is overwritten by Y; Temp is then copied into Y. With X = 1, Y = 2 this gives X = 2, Y = 1. Writing X ← Y first destroys the original X, so both end up 2. Temp ← X, Y ← Temp, X ← Y copies X into Y and then back, so both end up 1. Saving Y instead of X leaves Y unchanged and both end up 2.

Q6: A

VatRate is declared with CONSTANT, so its value cannot be changed while the program runs; the line VatRate ← 0.25 is an error (if the rate changes, edit the CONSTANT line instead). A price can have pence, so REAL is right. Total is on the left of the arrow, so it is being given a value, not used. OUTPUT may list text and variables separated by commas.

Lab and demo

What to project

The student lesson embeds these tabs. Open them full screen on the projector; every slider has a drag handle on the stage.

Slides

Slides for this lesson

A projectable deck for every pack is on the way. Until then, present the student lesson full screen: its sections, figure and lab tabs follow the same order as the plan above.

Also for this topic

Everything else you can use

Original work by the TheLucidSTEM team. The worksheet items are written in the style of the papers; no past paper question is reproduced.