Cambridge IGCSE · Topic 8.1
IGCSE Computer Science 0478 / Topic 8 / IGCSE

Name it, type it, store it.

Every program stores data while it runs: a score, a price, a name. Learn how to declare variables and constants in Cambridge pseudocode, choose the right data type for each item, and use INPUT and OUTPUT in a sequence of statements.

The key idea

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
Section 01

Variables and constants.

Run the totalling program in the lab one line at a time. The trace table records every new value of every variable: watch Total change on each pass while the instructions stay the same.

CONSTANT VatRate ← 0.2
DECLARE Price : REAL
DECLARE Total : REAL
INPUT Price
Total ← Price + Price * VatRate
OUTPUT "Total to pay: ", Total

The same idea in Python (for comparison only: the exam uses pseudocode)

VAT_RATE = 0.2          # Python has no true constants: capitals by convention
price = float(input())
total = price + price * VAT_RATE
print("Total to pay:", total)
Examiner trap

"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.

Section 02

Five data types.

Choose the data type from what the data is and what the program will do with it.

Data typeHoldsExample literalsTypical use
INTEGERa whole number5, -3, 0number of pupils, score
REALa number with a fractional part4.7, 0.3, -4.0price, height in metres
CHARa single character'x', '@'a grade such as 'B'
STRINGzero or more characters"Ali", ""name, postcode, phone number
BOOLEANTRUE or FALSETRUE, FALSEHasPaid, GameOver
Worked example Choosing data types
A club stores, for each member: their surname, their age in years, their height in metres, the first letter of their team name, whether they have paid, and their phone number. Choose a data type for each.
  1. Surname is text: STRING.
  2. Age in whole years: INTEGER.
  3. Height such as 1.62 needs a fractional part: REAL.
  4. One letter: CHAR. Paid or not: BOOLEAN.
  5. A phone number such as 01632 960123 starts with 0 and is never used in arithmetic: STRING.
Answer: STRING, INTEGER, REAL, CHAR, BOOLEAN, STRING
Your turn
Which data type should store the price of a cinema ticket, 7.50?
Examiner trap

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.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

Which description of a variable is correct?

Quick check+10 XP

In the lab's totalling program, the inputs are 5, 12, 7 and 3. What is output?

Quick check+10 XP

Which literal is of data type REAL?

Quick check+10 XP

Which data type should store whether a game is over?

Section 03

Input, output and sequence.

INPUT stores a value typed by the user in a variable; OUTPUT displays values. Several values can be output in one statement, separated by commas. The statements run in sequence, from top to bottom.

DECLARE Length : REAL
DECLARE Width : REAL
DECLARE Area : REAL
OUTPUT "Enter the length and width"
INPUT Length
INPUT Width
Area ← Length * Width
OUTPUT "Area is ", Area
Worked example Dry run
The program above is run and the user enters 4.5 and then 3.0. What is output?
  1. OUTPUT displays the prompt Enter the length and width.
  2. Length ← 4.5, then Width ← 3.0.
  3. Area ← 4.5 * 3.0 = 13.5.
  4. The last line outputs the text and then the value of Area.
Answer: Enter the length and width, then Area is 13.5
Worked example Order matters
A ← 5, then B ← A * 2, then A ← A + B, then OUTPUT A, " ", B. What is output?
  1. A ← 5.
  2. B ← 5 * 2 = 10.
  3. A ← 5 + 10 = 15 (B keeps its value 10).
  4. Output A, a space, then B.
Answer: 15 10
Your turn
What is output by A ← 3, A ← A * 4, B ← A - 2, OUTPUT B?
Examiner trap

OUTPUT "Total" displays the word Total; OUTPUT Total displays the value stored in the variable Total. Check your quotation marks.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to IGCSE Paper 1 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

Which data type is most suitable for a telephone number such as 01632 960123?

Exam style+20 XP

Which is a correct 0478 pseudocode declaration?

Exam style+20 XP

X ← 7, Y ← X, X ← 2, OUTPUT X + Y. What is output?

Exam style+20 XP

Why might a programmer use a constant for the number of seconds in a minute?

Skill unlocked

Variables, constants and data types, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Declaring variables and constants, choosing data types, INPUT and OUTPUT, and tracing short sequences of statements.