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.
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.
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
DECLARE <identifier> : <data type> names a variable and fixes its data type.Total ← Price + Price * VatRate works out the right-hand side, then stores the result in Total. With an input of 50.0 the output is Total to pay: 60.CONSTANT VatRate ← 0.2 is given a literal value once. If the rate changes, the programmer edits one line, and the program cannot change it by mistake.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)
"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.
Choose the data type from what the data is and what the program will do with it.
| Data type | Holds | Example literals | Typical use |
|---|---|---|---|
| INTEGER | a whole number | 5, -3, 0 | number of pupils, score |
| REAL | a number with a fractional part | 4.7, 0.3, -4.0 | price, height in metres |
| CHAR | a single character | 'x', '@' | a grade such as 'B' |
| STRING | zero or more characters | "Ali", "" | name, postcode, phone number |
| BOOLEAN | TRUE or FALSE | TRUE, FALSE | HasPaid, GameOver |
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.
Four quick checks. Each correct answer earns XP and records this skill on your profile.
Which description of a variable is correct?
In the lab's totalling program, the inputs are 5, 12, 7 and 3. What is output?
Which literal is of data type REAL?
Which data type should store whether a game is over?
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
A ← 5, then B ← A * 2, then A ← A + B, then OUTPUT A, " ", B. What is output?A ← 3, A ← A * 4, B ← A - 2, OUTPUT B?OUTPUT "Total" displays the word Total; OUTPUT Total displays the value stored in the variable Total. Check your quotation marks.
Unlocks once the four checks above are done. Worth more XP, written to IGCSE Paper 1 standard.
Which data type is most suitable for a telephone number such as 01632 960123?
Which is a correct 0478 pseudocode declaration?
X ← 7, Y ← X, X ← 2, OUTPUT X + Y. What is output?
Why might a programmer use a constant for the number of seconds in a minute?
This skill is now recorded on your profile. Keep the chain going.