Study Guide
Variables, Data Types and Operators
How a program remembers values: variables and constants, the five basic data types, input and output, instructions carried out in sequence, and the arithmetic, relational and logical operators, all in Cambridge pseudocode with Python alongside.
Lesson 5.3 Algorithms and Programming
For: Complete beginners to programming: Key Stage 3 (about ages 11 to 14), or a Cambridge IGCSE Computer Science course
Stage: Think
Remembering a score
In a fictional game called Meteor Dash, the player’s score starts at zero and goes up by 10 each time they collect a coin. While the game is running, the computer has to remember the score from one moment to the next.
Think it through
What must the computer be able to do with that remembered score? And what would you call the place where it is kept?
- Hint 1: What happens when the player collects a coin?
- Hint 2: What happens when the score is shown on screen?
Show a model answer
It must be able to read the score (to show it), and to change it (to add 10). It also needs a name for the place, so that the program can say which value it means. In programming, a named place that holds a value which can change is called a variable.
Stage: Predict
Variables and assignment
A variable is a named place in the computer’s memory that holds one value, which the program can change. Storing a value in a variable is called assignment, and in Cambridge pseudocode it is written with an arrow, ←. Here is Meteor Dash’s score being updated twice:
Score ← 0
Score ← Score + 10
Score ← Score + 10
OUTPUT ScoreThink it through
Predict what this program outputs, and explain what the second line does. Note that Score appears on both sides of the arrow.
- Hint 1: Work from the top. What is stored in Score after line 1?
- Hint 2: To carry out line 2, the computer first works out the right-hand side using the current value.
Show a model answer
It outputs 20. Line 1 stores 0. In line 2 the computer works out the right-hand side first: the current value of Score (0) plus 10 is 10, and that is stored back in Score, replacing the old value. Line 3 does the same again, giving 20.
The rule: the right-hand side is worked out first, using the values the variables hold now, and the result is stored in the variable on the left, replacing what was there. Follow the program in a trace table. A blank cell means that nothing changes or nothing is output on that line.
| Line carried out | Score | OUTPUT |
|---|---|---|
| Score ← 0 | ||
| Score ← Score + 10 | ||
| Score ← Score + 10 | ||
| OUTPUT Score |
Show the completed table
| Line carried out | Score | OUTPUT |
|---|---|---|
| Score ← 0 | 0 | |
| Score ← Score + 10 | 10 | |
| Score ← Score + 10 | 20 | |
| OUTPUT Score | 20 |
In mathematics, Score = Score + 10 can never be true. In pseudocode, Score ← Score + 10 is not an equation: it is an instruction that means “take the current value of Score, add 10 and store the answer in Score”. The arrow works in one direction only: Score ← 5 stores 5 in Score, but 5 ← Score means nothing.
Check your understanding
Show the answer
Answer: C. 11
Each assignment replaces the old value with the new one.
Stage: Discover
Constants, declarations and names
The game awards 10 points for a coin, and the number 10 is typed into forty different places in the code. The designers decide a coin should be worth 15.
Think it through
What problem will the designers have, and how could the program have been written to avoid it?
- Hint 1: If they miss one of the forty places, what will the game do?
Show a model answer
They would have to find and change all forty places, and missing one would make the game give different points in different places. It would be better to give the value a name once, and use the name everywhere. Then it is changed in one place only.
A value that is set once and never changes while the program runs is a constant. A variable’s value is meant to change; a constant’s is not. In Cambridge pseudocode:
CONSTANT PointsPerCoin ← 10
DECLARE Score : INTEGER
DECLARE PlayerName : STRING
Score ← Score + PointsPerCoinCONSTANTgives a name to a fixed value. Its value must be written out as a plain value (a literal such as 10 or “N/A”): it cannot be a variable or a calculation.DECLAREsays that a variable exists, and which data type it holds (the next step).- The names the programmer chooses are called identifiers. They use letters and digits only, start with a capital letter, and use a capital for each new word (
NumberOfPlayers). There are no spaces or underscores, and they should describe what they hold. A keyword such asOUTPUTcannot be a name.
Check your understanding
Show the answer
Answer: A. TotalToPay
Good names describe what the value is, so the program can be read like a description.
Check your understanding
Show the answer
Answer: B. The value can be changed in one place, and the name shows what the number means.
Constants make a program easier to read and to update.
Stage: Discover
Kinds of value
Here are ten values: 42, 3.5, "Ali", 'A', TRUE, "42", -7, 0.0, "Hello World", FALSE.
Think it through
Sort these ten values into groups of the same kind of thing. How many groups do you make, and what would you call each?
- Hint 1: Notice how some values are written with quotation marks and some are not.
- Hint 2: Is 42 the same kind of thing as "42"?
Show a model answer
One sensible grouping is five groups: whole numbers (42, -7); numbers with a fractional part (3.5, 0.0); text (“Ali”, “42”, “Hello World”); one single character (‘A’); and true-or-false values (TRUE, FALSE). Notice that "42" has quotation marks, so it is text, not a number, even though it looks like one.
Every value has a data type: the kind of value it is. The type decides what the program can do with it, such as add it, join it to other text or test whether it is true. The syllabus uses five basic types:
| Data type | Holds | Written as | Example variable |
|---|---|---|---|
Integer (INTEGER) | A whole number, positive, negative or zero | 5, -3 | NumberOfLives |
Real number (REAL) | A number that can have a fractional part, always written with a digit on both sides of the point | 4.7, 0.3, -4.0, 0.0 | Height |
Char (CHAR) | One single character, in single quotes | 'x', '@' | Grade |
String (STRING) | Zero or more characters, in double quotes; "" is the empty string | "Ali", "Hello World" | PlayerName |
Boolean data type (BOOLEAN) | Only TRUE or FALSE | TRUE, FALSE | GameOver |
A variable is declared with its type, as in DECLARE Score : INTEGER, and then holds only values of that type.
A number written in quotation marks is text. "42" is a STRING: the computer sees two characters, a 4 and a 2, and cannot add them. 42 is an INTEGER. Use a number type when the value will be added, compared or averaged, and STRING for anything that is only ever displayed or stored as text.
Check your understanding
Show the answer
Answer: D. STRING, because it is a label, not a quantity, and the leading 0 must be kept.
Choose the type from how the value is used, not from how it looks.
Choose the best type for each variable in a fictional game’s code.
| Variable and what it holds | Data type |
|---|---|
| NumberOfLives: 3 | |
| PlayerName: "Zara" | |
| Height: 1.62 (metres) | |
| GameOver: TRUE | |
| Grade: ’B’ | |
| PostCode: "LS1 4AB" |
- The five types are INTEGER, REAL, CHAR, STRING and BOOLEAN.
Show the completed table
| Variable and what it holds | Data type |
|---|---|
| NumberOfLives: 3 | INTEGER |
| PlayerName: "Zara" | STRING |
| Height: 1.62 (metres) | REAL |
| GameOver: TRUE | BOOLEAN |
| Grade: ’B’ | CHAR |
| PostCode: "LS1 4AB" | STRING |
Stage: Predict
Input, output and sequence
A program gets values from the user with INPUT and shows values with OUTPUT. INPUT Name waits for the user to type and stores what they typed in the variable Name. OUTPUT can show several values in one go, separated by commas: they are joined together with nothing added between them, so any spaces must be put inside the quotation marks. Here is a short fictional program:
DECLARE Name : STRING
DECLARE Age : INTEGER
OUTPUT "What is your name?"
INPUT Name
OUTPUT "How old are you?"
INPUT Age
Age ← Age + 1
OUTPUT Name, " will be ", Age, " next year"Think it through
A user types Maya and then 12. Write down everything the program shows on the screen, in order.
- Hint 1: Count the OUTPUT lines. There are three.
- Hint 2: Which value does Age hold when the last line runs?
Show a model answer
The screen shows three lines:
What is your name?
How old are you?
Maya will be 13 next yearThe Age that was typed is 12; line 7 adds 1 and stores 13, and the last line joins Name, the text, Age and the text with nothing added between them.
Instructions are carried out one after another, in the order they are written: this is called sequence. Check your prediction with a trace table (the user types Maya, then 12).
| Line carried out | Name | Age | OUTPUT |
|---|---|---|---|
| OUTPUT "What is your name?" | |||
| INPUT Name | |||
| OUTPUT "How old are you?" | |||
| INPUT Age | |||
| Age ← Age + 1 | |||
| OUTPUT Name, " will be ", Age, " next year" |
Show the completed table
| Line carried out | Name | Age | OUTPUT |
|---|---|---|---|
| OUTPUT "What is your name?" | What is your name? | ||
| INPUT Name | Maya | ||
| OUTPUT "How old are you?" | How old are you? | ||
| INPUT Age | 12 | ||
| Age ← Age + 1 | 13 | ||
| OUTPUT Name, " will be ", Age, " next year" | Maya will be 13 next year |
Apply it
Show the answer
Answer: C. 14, because 8 - 1 = 7 is stored first and then doubled.
In a sequence the same instructions in a different order can give a different result.
Stage: Discover
Arithmetic operators
An operator is a symbol or word that works on values to give a result. Start with a sharing problem.
Think it through
17 sweets are shared out equally between 5 children, and nobody may be given a part of a sweet. How many does each child get, and how many are left over?
- Hint 1: How many times does 5 go into 17 completely?
Show a model answer
Each child gets 3 sweets (3 × 5 = 15), and 2 sweets are left over (17 - 15). Dividing like this, to a whole number and a remainder, is something programs do all the time.
The arithmetic operators in the syllabus are:
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | add | 7 + 2 | 9 |
- | subtract | 7 - 2 | 5 |
* | multiply | 7 * 2 | 14 |
/ | divide (the result may have a fractional part) | 7 / 2 | 3.5 |
^ | raise to a power | 2 ^ 3 | 8 |
DIV | divide and keep only the whole number of times | DIV(17, 5) | 3 |
MOD | divide and keep only the remainder | MOD(17, 5) | 2 |
DIV(17, 5) is the number of children’s sweets, and MOD(17, 5) is the number left over. When a calculation has several operators, powers are worked out first, then * and /, then + and -; brackets are worked out before everything else. So 2 + 3 * 4 is 14, but (2 + 3) * 4 is 20. Putting brackets in makes the order clear, even when they are not needed.
- Decide what is needed. Whole hours and the minutes left over. “Whole” and “left over” point to DIV and MOD.
- Hours. There are 60 minutes in an hour, so the whole hours are
DIV(130, 60). 60 goes into 130 twice (120), so Hours is 2. - Minutes. What is left after taking out the whole hours is
MOD(130, 60): 130 - 120 = 10, so Minutes is 10. - Check. 2 hours is 120 minutes, and 120 + 10 = 130. The answer rebuilds the original number, so it is right.
Using / would have given 2.1666…, which does not say how many whole hours and minutes there are. That is why DIV and MOD are needed.
Check your understanding
Show the answer
Answer: A. DIV(237, 50)
DIV counts how many whole times; MOD gives what is left over.
Check your understanding
Show the answer
Answer: C. MOD(Number, 2) = 0
A remainder of 0 when dividing by 2 means the number is even.
Work out each expression. Write the result.
| Expression | Result |
|---|---|
| 14 + 3 * 2 | |
| (14 + 3) * 2 | |
| 2 ^ 4 | |
| DIV(23, 4) | |
| MOD(23, 4) | |
| 9 / 2 |
Show the completed table
| Expression | Result |
|---|---|
| 14 + 3 * 2 | 20 |
| (14 + 3) * 2 | 34 |
| 2 ^ 4 | 16 |
| DIV(23, 4) | 5 |
| MOD(23, 4) | 3 |
| 9 / 2 | 4.5 |
Stage: Explain
Comparing values and combining tests
In Lesson 2 you used symbols such as <= in a condition. A test like Score > 100 is true or false, and the result is a value of the BOOLEAN data type.
Think it through
A theme-park ride is for visitors who are at least 12 and at most 18 years old. A visitor is 14. Is the visitor allowed on? Write the rule using a comparison for each end of the range.
- Hint 1: There are two tests, and the visitor must pass both.
Show a model answer
Yes, 14 is allowed. The rule needs two comparisons: Age >= 12 and Age <= 18, and both must be true. They are combined with AND: Age >= 12 AND Age <= 18.
The relational operators compare two values and give TRUE or FALSE: = (equal to), <> (not equal to), <, <=, > and >=. The logical operators combine TRUE and FALSE values: AND is TRUE only when both sides are TRUE; OR is TRUE when at least one side is TRUE; NOT turns TRUE into FALSE and FALSE into TRUE. They work exactly like the AND, OR and NOT gates, with TRUE and FALSE in place of 1 and 0. Put brackets round the parts of a long condition to make the order clear.
Each side of AND or OR must be a complete test of its own. The two range tests are written Age >= 12 AND Age <= 18. Writing Age >= 12 AND <= 18 (leaving out the second Age) or 12 <= Age <= 18 is not valid pseudocode.
Apply it
Show the answer
Answer: B. The right-hand side has no value to compare: it should be Age >= 12 AND Age <= 18.
Repeat the variable on each side of AND or OR.
A visitor has Age 14 and HasTicket TRUE. Work out each condition. Write TRUE or FALSE.
| Condition | TRUE or FALSE? |
|---|---|
| Age >= 12 | |
| Age >= 12 AND Age <= 18 | |
| Age < 12 OR Age > 65 | |
| NOT HasTicket | |
| Age <> 14 | |
| HasTicket AND Age > 16 |
Show the completed table
| Condition | TRUE or FALSE? |
|---|---|
| Age >= 12 | TRUE |
| Age >= 12 AND Age <= 18 | TRUE |
| Age < 12 OR Age > 65 | FALSE |
| NOT HasTicket | FALSE |
| Age <> 14 | FALSE |
| HasTicket AND Age > 16 | FALSE |
Stage: Apply
A complete program
A school canteen till (fictional) sells snacks at 85 pence each. It takes the number of snacks and shows the total in pounds and pence, using whole numbers of pence to avoid fractions.
CONSTANT SnackPrice ← 85
DECLARE Quantity : INTEGER
DECLARE Total : INTEGER
DECLARE Pounds : INTEGER
DECLARE Pence : INTEGER
INPUT Quantity
Total ← Quantity * SnackPrice
Pounds ← DIV(Total, 100)
Pence ← MOD(Total, 100)
OUTPUT Pounds, " pounds and ", Pence, " pence"Apply it
Show the answer
Answer: D. 3 pounds and 40 pence
DIV gives the whole pounds and MOD gives the pence left over.
Think it through
Write a program in Cambridge pseudocode that declares what it needs, inputs two test marks (whole numbers out of 20) and outputs their average. Mention which data type the average needs, and why.
Work this out on paper or the board first. Nothing here is marked: compare yours with the model answer.
- Hint 1: What do you get if you add the two marks and divide by 2? Could it have a fractional part?
Show a model answer
DECLARE MarkOne : INTEGER
DECLARE MarkTwo : INTEGER
DECLARE Average : REAL
INPUT MarkOne
INPUT MarkTwo
Average ← (MarkOne + MarkTwo) / 2
OUTPUT AverageAverage is a REAL because dividing by 2 can give a fractional part: marks of 15 and 18 give 16.5. The brackets make the addition happen before the division; without them only MarkTwo would be divided by 2.
Go deeper The same ideas in Python
Python is a real programming language, so a computer can run these. The ideas are the same; the notation differs.
SNACK_PRICE = 85
quantity = int(input())
total = quantity * SNACK_PRICE
pounds = total // 100
pence = total % 100
print(pounds, "pounds and", pence, "pence")- Python has no
DECLARE: a variable exists from its first assignment, and its type comes from the value stored. =stores a value (pseudocode uses ←).==tests for equal,!=for not equal;and,orandnotare in lower case.//is DIV and%is MOD.**raises to a power (2 ** 4is 16), where pseudocode uses^. Python’s/always gives a real number:9 / 2is4.5and8 / 2is4.0.input()always gives a string, soint(...)converts it, orfloat(...)for a real number. Forgetting this is a common error: adding a string and a number stops the program.- Python has no true constant. By convention, a name in capital letters such as
SNACK_PRICEis not changed. - Python has no separate CHAR: a single character is simply a string of length 1, and either
'x'or"x"works. printis Python’s OUTPUT; commas inprintput a space between the values, but pseudocode’s OUTPUT does not.
Stage: Consolidate
Show what you know
Think it through
Explain to a friend the difference between a variable and a constant, with one example of each from a school canteen till.
Show a model answer
Apply it
Show the answer
Answer: A. BOOLEAN, because it has only two possible values, TRUE or FALSE.
When a value has only two possible answers, BOOLEAN is the safest type.
- A variable is a named place that holds a value that can change; an assignment
Score ← Score + 10works out the right side first and stores the result on the left. A constant is set once and not changed. DECLARE Name : TYPEsays a variable exists. Names start with a capital letter and use capitals for new words.- A data type is the kind of value:
INTEGER(whole numbers),REAL(numbers with a fractional part),CHAR(one character),STRING(text) andBOOLEAN(TRUE or FALSE). INPUTstores what the user types;OUTPUTshows values. Instructions run in sequence, so their order matters.- Operators:
+ - * / ^,DIV(whole number of times) andMOD(remainder); the relational operators= <> < <= > >=give TRUE or FALSE;AND,ORandNOTcombine them, and each side of AND and OR must be a complete test. - Python writes the same ideas differently:
//for DIV,%for MOD,==for “equal to”, and no DECLARE.
Sources and scope
- What this lesson is. The third lesson of the Algorithms and Programming module, written for this site. It covers points 8.1.1, 8.1.2, 8.1.3, 8.1.4(a) and 8.1.4(f) of Cambridge IGCSE Computer Science 0478 (2026 to 2028, version 6). Its curriculum links are provisional and imply no review or endorsement by any examination board.
- Original material. Meteor Dash, Maya, Zara, the theme-park ride, the canteen till, the shop program and Sam are fictional, written for this lesson. The pseudocode follows the layout in the syllabus’s pseudocode guide, and each program’s output was worked out by hand.
- Simplified on purpose. Money is held in whole pence to keep the arithmetic in whole numbers. The rules for the order of the logical operators are not taught; brackets are used instead, as the syllabus advises. A real program would also check that the user types a number where one is needed; that is left to a later lesson on validation.
- Left to later lessons. Selection (Lesson 4), iteration (Lesson 5), totalling and counting (Lesson 6). String handling, arrays, subroutines and library routines such as ROUND and RANDOM are not covered in this lesson.
A sequence of precise steps that solves a problem or completes a task.
A statement that stores a value in a variable. In Cambridge pseudocode it is written with an arrow, for example Score ← 0.
The data type (BOOLEAN) with only two possible values, TRUE and FALSE.
The data type (CHAR) for one single character, written between single quotes, such as ‘x’.
An expression that is either true or false, such as Age >= 12, used to decide what a program does next.
A named value that is set once and does not change while the program runs.
The kind of value a variable holds, such as a whole number or text. It decides what the program can do with the value.
The data type for whole numbers, positive, negative or zero, such as 5 or -3.
Repeating instructions. A loop does this.
A logic gate with one input, whose output is the opposite of its input: 1 becomes 0 and 0 becomes 1.
A symbol or word that works on values to give a result, such as +, > or AND.
A precise but language-independent way of writing an algorithm, with set keywords such as IF, WHILE and OUTPUT, that is not tied to any one programming language.
A high-level programming language written as text, popular for learning and for real work, in which indentation shows which lines belong together.
The data type (REAL) for numbers that can have a fractional part, always written with at least one digit either side of the point, such as 4.7 or 0.0.
Choosing which instructions to carry out, depending on whether a condition is true. IF and CASE statements do this.
Instructions carried out one after another, in the order they are written.
The data type (STRING) for text: zero or more characters written between double quotes, such as “Hello”.
A table used to follow an algorithm by hand, with a column for each variable and for the output and a row for each step, to record how the values change.
A named place in memory that stores a value which can change while a program runs.
Your progress on this lesson
Saved in this browser only. It is not sent anywhere, does not follow you to another device, and is lost if you clear this site’s data.
This browser is not allowing the site to save progress, so it will be forgotten when you leave the page.