Programming Concepts
Programming · 4 question types
Variables, Constants and Data Types
Every program manipulates data: numbers, text, true/false values. To work with data, the program stores values in named (which can change) and (which cannot).
Variables
A variable is a named storage location whose value can change while the program is running.
In CIE pseudocode, a variable is declared with DECLARE and a data type:
DECLARE Age : INTEGER
DECLARE Price : REAL
DECLARE Name : STRING
DECLARE FirstInitial : CHAR
DECLARE GameOver : BOOLEAN
After declaration, a value is assigned with the arrow operator ←:
Age ← 17
Price ← 9.99
Name ← "Alice"
FirstInitial ← 'A'
GameOver ← FALSE
Declaring a variable in pseudocode
Question: Write the pseudocode statement that declares a named variable of a given data type, sometimes followed by statements that store a value in it (1–2 marks, or one mark point of several).
Asked in 4 of the 17 papers. The declaration is DECLARE <identifier> : <data type>, with the colon and the type keyword in capitals, as in DECLARE Message : STRING; asked alone, it is worth one mark. In the two questions where it is one step of a text-file task, declaring the string variable is one of the mark points, so write it first even when the wording seems to assume the variable already exists.
When you also store a value, assign it with ← and match the quotes to the type: double quotes for a STRING (Title ← "Go") and single quotes for a CHAR (Grade ← 'A'). One scheme pays two marks for two declarations and their two assignments, one for every two correct lines.
Constants
A constant is a named storage location whose value is set once and cannot change for the rest of the program.
CONSTANT Pi ← 3.142
CONSTANT Vat ← 0.20
CONSTANT MaxLives ← 3
Constants are named in the same PascalCase style as variables, and they make programs easier to maintain: if the VAT rate changes, only one line of the program has to be edited.
What variables and constants are for
Question: Explain how a program should use variables and constants, complete a sentence that defines a constant, or circle the words that name places where data can be stored (1–3 marks).
Asked in 3 of the 17 papers. A variable holds a value that can change while the program runs; a constant holds a value that is set once and never changes during execution. Both are places where data is stored, and so is an array: those are the three words to circle, while input, output, procedure and dimension are not storage.
The explain version pays one mark per point up to three: both store data; constants are for values that must never change, such as a tax rate; variables hold values the program calculates or changes as it runs; and both should have meaningful identifiers so that other programmers understand their purpose. The tick-box distractors describe a variable (a value the program may change whenever it likes), an array (values of one data type) and a store for mixed data types, so read every option first.
Naming conventions
Identifiers (the names of variables and constants) should:
- Use letters and digits only, no spaces.
- Start with a capital letter, not a digit.
- Be in PascalCase: each word capitalised, e.g.
TotalScore. - Be descriptive:
StudentScoreis far better thansorx. - Never contain an underscore, an accented letter or any other symbol — and this applies to constant names too.
Suggesting meaningful identifiers for an algorithm
Question: The algorithm uses single-letter names: suggest a meaningful identifier for its array and for each of three variables (3 marks).
Asked in 3 of the 17 papers, always as a later part of a longer pseudocode question. One mark is for the array's name, two for all three variable names and one for two of them (two of the three schemes give it for just one). Read what the algorithm does with each name before you choose: the variable a FOR or WHILE loop steps through is Index or Counter, one that goes up by one each time is Counter, a running sum is Total, a value parked during a swap is Temp, the number of items is Length, a mean is Average, and the array takes the name of what it holds, such as Names or Values.
Names are judged on meaning, so even a plain MyArray was credited for an array. Write them in the pseudocode style, with a capital first letter and no spaces.
Making a program easier to understand and maintain
Question: State features a programmer can use so that another programmer can understand and maintain a program, sometimes with an example of each (2–6 marks).
Asked in 2 of the 17 papers. The credited features are meaningful identifiers, comments that explain what the code does, procedures and functions for the separate tasks, and white space such as blank lines and indentation. When an example is asked for, it earns a mark of its own, so make it concrete: a name like RunningTotal, a comment line such as // reset the counters for the next customer, or a procedure call such as CALL PrintReceipt(Total).
When the question follows one on identifiers and asks for other ways, leave identifiers out and give two of the remaining three.
Data types
There are five basic data types. Choosing the right one for each variable makes the program more accurate and uses memory efficiently.
| Data type | Used for | Pseudocode keyword | Examples |
|---|---|---|---|
| Integer | Whole numbers (no fractional part) | INTEGER | 10, -5, 0, 1000 |
| Real | Numbers with a fractional part | REAL | 3.14, -2.5, 0.0 |
| Char | A single character | CHAR | 'a', 'B', '7', '£' |
| String | A sequence of characters | STRING | "Hello world", "ABC", "" |
| Boolean | A logical value | BOOLEAN | TRUE, FALSE |
Notes on the difference between and string:
- A
CHARis exactly one character, written in single quotes ('A'). - A
STRINGis zero or more characters, written in double quotes ("Apple").
Casting
Sometimes a value needs to be converted from one type to another. This is called . For example, in a language such as Python a number typed by the user arrives as a string and may need to be converted to an integer before arithmetic can be done on it.
Common slip: in Python, adding
"5"to"3"as strings gives"53"(concatenation), not8. Cast to integer first.
Picking the right data type
A short reference for matching everyday data to the most suitable CIE type:
| Kind of value | Best type | Quick test |
|---|---|---|
| Whole-number counts (age, score, year) | INTEGER | No decimal point ever needed |
| Measurements and money (1.75 m, £8.50) | REAL | A fractional part is possible |
| Names, addresses, sentences | STRING | More than one character, or any text |
A single flag character ('M', 'F', 'Y', 'N') | CHAR | Exactly one character |
| Yes/no, on/off, paid/unpaid | BOOLEAN | Two possible values: TRUE or FALSE |
When two types look possible, prefer the smaller one. A house number is an INTEGER, not a STRING. A pH reading is a REAL, not a STRING. Storing numbers as strings means losing the ability to compare or do arithmetic on them later.
Identifying and defining the basic data types
Question: Link four descriptions of data to the best-fitting data type from a list of five, or define the integer and real data types and give an example of each (4 marks).
Asked in 2 of the 17 papers, one mark per line or per point. The credited matches follow the definitions above: a whole number is INTEGER, a number with a fractional part, such as one with two decimal places, is REAL, a single letter is CHAR, and a word or phrase is STRING. BOOLEAN, which holds only TRUE or FALSE, is the type left unused when every description is a number or some text.
When you define integer and real, the example is a separate mark from the statement, so write an actual value: 42 for an integer and 3.75 for a real. The question asks for the most appropriate type, so a single letter goes to CHAR rather than STRING, and anything with decimal places goes to REAL, never INTEGER.