Topic 8 · Programming
Variables, constants, data types, and input/output
Store values in variables and constants, choose the right data type, and use INPUT and OUTPUT.
Variables are labelled boxes
A variable is a named location in memory that stores a value. The value can change while the program runs. Think of it as a labelled box: the name is on the label, the value is inside.
In pseudocode, declare each variable with its data type, then use <- to put a value into it. Assignment replaces whatever was in the box before.
DECLARE Score : INTEGER
Score <- 10
Score <- Score + 5
OUTPUT ScoreKey idea. Score <- Score + 5 means: take the value in Score, add 5, and put the answer back in Score. It is not an equation.
Constants
A constant is a named value that cannot change while the program runs. Using a constant means the value is written in one place, so it is easy to update and impossible to change by accident.
CONSTANT VatRate <- 0.2
DECLARE Price : REAL
INPUT Price
OUTPUT "Price with VAT: ", Price * (1 + VatRate)Data types
Every variable has a data type that sets what it can hold:
| Type | Holds | Examples |
|---|---|---|
INTEGER | Whole numbers | 42, -7, 0 |
REAL | Numbers with a decimal part | 3.14, -0.5, 19.99 |
CHAR | A single character | 'A', '7', '?' |
STRING | Text: zero or more characters | "Hello", "07700 900123" |
BOOLEAN | TRUE or FALSE | TRUE, FALSE |
Exam tip. Phone numbers are stored as STRING, not INTEGER: they can start with 0, contain spaces, and you never do maths with them.
Input and output
INPUT waits for the user to type a value and stores it in a variable. OUTPUT displays values. Separate several values with commas.
In PseudoEditor, the terminal asks for each INPUT while the program runs, so you can test interactive programs in the browser.
DECLARE Name : STRING
DECLARE Age : INTEGER
OUTPUT "What is your name?"
INPUT Name
OUTPUT "How old are you?"
INPUT Age
OUTPUT "Hello ", Name, ", next year you will be ", Age + 1Sequence
Sequence means statements run one after another, in order, from top to bottom. Changing the order changes the result: in the program above, the age must be input before it is used.
Key terms
- Variable
- A named memory location whose value can change while the program runs.
- Constant
- A named value that cannot change while the program runs.
- Data type
- The kind of data a variable holds, such as INTEGER or STRING.
- Sequence
- Instructions executed one after another in order.
Check yourself
What data type should store whether a door is locked?Show answer
BOOLEAN, since it is either TRUE or FALSE.
What is output by: X <- 4, X <- X * 3, OUTPUT X?Show answer
12.
Give one reason to use a constant instead of typing the value each time.Show answer
If the value changes it only needs updating in one place, and it cannot be changed by mistake while the program runs.
Practise this skill
Use what you learned in a question, then run your answer in the studio editor.