Syllabus coverage
- Declare and use variables and constants
- Understand and use the basic data types: integer, real, char, string and Boolean
- Understand and use input and output
- Understand and use sequence, selection (IF and CASE statements) and iteration (count-controlled, pre-condition and post-condition loops)
- Understand and use nested statements, including nested selection and nested iteration
- Understand and use arithmetic, relational and logical operators
- Understand and use library routines
- Understand how to create a maintainable program through meaningful identifiers, comments, procedures and functions
Part of the O Level Computer Science syllabus 2210.
Variables and constants
A variable is a named store whose value can change while the program runs. In pseudocode it is declared before it is used, with its data type: DECLARE Score : INTEGER. The declaration tells the reader what kind of value the name will hold, and a value is put into it with the assignment arrow, Score ← 0. A variable that has been declared but not yet assigned has no usable value, which is why totals and counts are always set to 0 before a loop.
A constant is a named store whose value is fixed when the program is written and cannot be changed while it runs. It is written as CONSTANT VATRate ← 0.20. Only a literal may be used as the value of a constant, never a variable or an expression, because the value has to be known before the program starts.
Constants are not a decoration. If the rate of VAT appears as 0.20 in twelve different places and the rate changes, someone has to find all twelve and change them, and will miss one. Declared once as a constant, it is changed in one place. The same argument applies to a maximum class size, a board size in a game or a conversion factor.
Identifiers are the names given to variables, constants, procedures and functions. In 2210 they are written in mixed case with a capital at the start of each word, contain only letters and digits, and never begin with a digit or use an underscore. TotalScore is an identifier; total_score and 2ndScore are not.
- Declare every variable with its data type before it is used.
- Assign with ←, never with =; the equals sign is a comparison in this pseudocode.
- Use a constant for any fixed value that appears more than once or might change later.
The five basic data types
A data type fixes what kind of value a variable may hold and what can be done with it. 2210 limits the basic types to five: INTEGER, REAL, CHAR, STRING and BOOLEAN. Choosing the type badly causes problems later: a telephone number stored as an INTEGER loses its leading zero, and a price stored as an INTEGER loses its pence.
An INTEGER is a whole number, positive, negative or zero, such as 42. A REAL is a number with a fractional part and is always written with a decimal point and at least one digit on each side, such as 3.75 or 0.0. A CHAR is a single character in single quotes, such as 'A' or '7'. A STRING is zero or more characters in double quotes, such as "Wednesday" or the empty string. A BOOLEAN holds only TRUE or FALSE, which is the type produced by every comparison.
The type also decides which operations are allowed. Arithmetic works on INTEGER and REAL, joining works on STRING, and AND, OR and NOT work only on BOOLEAN values. LENGTH, SUBSTRING, UCASE and LCASE apply to strings, while MOD, DIV and ROUND apply to numbers.
| Data type | What it holds | Example literal | Typical use |
|---|---|---|---|
| INTEGER | A whole number | 42 | A quantity ordered, a number of pupils, a score |
| REAL | A number with a fractional part | 3.75 | A price, a temperature, an average |
| CHAR | A single character | 'A' | A menu option, a grade letter, a yes/no answer |
| STRING | Zero or more characters | "Wednesday" | A name, an address, a password |
| BOOLEAN | TRUE or FALSE only | TRUE | A flag such as Found, or the result of a comparison |
Input and output
INPUT takes a value from the user and stores it in a variable: INPUT PupilName. OUTPUT displays one or more values: OUTPUT "The total is ", Total. Several values can be printed by one OUTPUT statement if they are separated by commas, and text in double quotes is printed exactly as it is written.
A good program tells the user what to type before it waits for the input, so a prompt is written as its own OUTPUT statement immediately before the INPUT: OUTPUT "Enter the price of the item", then INPUT Price. Without the prompt the program appears to hang, and the user has no idea what is expected.
Input is where most bad data enters a system, so input statements are usually followed by validation. The INPUT statement itself accepts whatever is typed; it is the check that follows which decides whether the value is allowed.
Sequence: statements in the order they are written
Sequence is the default: the statements are carried out one after another, from top to bottom, and the order matters. Total ← 0 followed by Total ← Total + Price gives a total of Price; the same two statements the other way round throw away the first value.
Assignment is not an equation. Total ← Total + 1 means 'work out the value of Total + 1 and store the result back in Total', which is why it makes sense as a statement even though it would be nonsense as algebra. The arrow always points at the variable being changed.
Sequence is also what makes a swap work. To exchange the values of X and Y you need a third variable: Temp ← X, then X ← Y, then Y ← Temp. Without Temp, the first assignment destroys the value that the third one needs, and both variables end up holding the same value.
Selection: IF, CASE and nesting
Selection chooses between paths. An IF statement tests a condition and runs its statements only when the condition is TRUE. The THEN sits on its own line, the statements are indented, and the whole statement is closed with ENDIF. An ELSE clause supplies the path taken when the condition is FALSE. Every IF needs its ENDIF, however short it is.
A CASE statement chooses between several branches on the value of one variable. It begins CASE OF Identifier, each case is a single value followed by a colon and one statement, and an OTHERWISE clause at the end catches any value that matches none of the cases. The cases are tested in order and the first match is carried out; the remaining cases are then skipped, and control passes to the statement after ENDCASE.
Nesting means putting one statement inside another. An IF inside an IF allows a second question to be asked only when the first has been answered in a particular way, and 2210 limits nested statements to three levels, which is enough for almost every problem but not so deep that the logic becomes impossible to follow. Nesting is shown by indentation: each level moves two spaces further in, and the ENDIF that closes the outer IF lines up with it.
| Construct | When to use it | Shape in pseudocode |
|---|---|---|
| IF without ELSE | Something happens only when the condition is true | IF ... THEN ... ENDIF |
| IF with ELSE | One of exactly two paths is taken | IF ... THEN ... ELSE ... ENDIF |
| Nested IF | A second decision is needed on one of the paths | An IF statement written inside a THEN or ELSE clause |
| CASE | One of several paths is taken, chosen by the value of one variable | CASE OF ... value : statement ... OTHERWISE ... ENDCASE |
Iteration: three loops and how to choose between them
Iteration repeats statements. There are three forms in 2210, and choosing the right one is part of the answer.
A count-controlled loop repeats a known number of times: FOR Index ← 1 TO 10, the statements, then NEXT Index. The counter is an INTEGER, both end values are included, and if the first value is larger than the second the statements are not carried out at all. A STEP can be added, and the step may be negative, as in FOR Index ← 10 TO 1 STEP -1.
A pre-condition loop is written WHILE condition DO ... ENDWHILE and tests its condition before the statements run. It therefore runs zero or more times, and it repeats while the condition is TRUE. A post-condition loop is written REPEAT ... UNTIL condition and tests its condition after the statements, so it always runs at least once and repeats until the condition becomes TRUE. The two are opposites in both position and sense, and swapping them is a common error.
Choose a FOR loop when the number of repetitions is known before the loop starts, a WHILE loop when it might be zero and the condition can be tested first, and a REPEAT loop when the statements must run at least once, such as showing a menu before asking whether the user wants to quit.
Loops nest, with an inner loop completing all of its passes for each single pass of the outer loop. Two nested FOR loops over rows and columns are how a table is filled or searched. Totalling and counting are the two standard jobs done inside a loop: Total ← Total + Value and Count ← Count + 1, with both variables set to 0 before the loop begins.
| Loop | Condition tested | Minimum number of runs | Use it when |
|---|---|---|---|
| FOR ... NEXT | By the counter, before each pass | Zero, if the start is past the end | The number of repetitions is known |
| WHILE ... DO ... ENDWHILE | Before the statements | Zero | The loop may need to be skipped entirely |
| REPEAT ... UNTIL | After the statements | One | The statements must run at least once |
Arithmetic, relational and logical operators
Arithmetic operators in 2210 are +, -, *, / and ^ for a power, together with MOD and DIV, which work on integers only. DIV gives the whole-number quotient and discards the remainder; MOD gives the remainder. DIV(17, 5) is 3 and MOD(17, 5) is 2. Multiplication and division are carried out before addition and subtraction, and brackets can be used to make the order explicit, which is good practice in a long expression.
Relational operators compare two values and always produce a BOOLEAN: = for equal to, <> for not equal to, <, <=, > and >=. They are what goes inside the condition of an IF or a loop. A single = is a comparison in pseudocode, never an assignment.
Logical operators are AND, OR and NOT, and they combine BOOLEAN values. AND is true only when both parts are true, OR is true when at least one part is true, and NOT reverses a value. They are how a program tests two conditions at once, as in IF Age >= 11 AND Age <= 18, where both comparisons must hold. Brackets make the meaning clear when AND and OR are mixed.
| Group | Operators | Example and result |
|---|---|---|
| Arithmetic | + - * / ^ | 3 + 4 * 2 gives 11, because multiplication happens first |
| Integer division | DIV, MOD | DIV(17, 5) gives 3; MOD(17, 5) gives 2 |
| Relational | = <> < <= > >= | Mark >= 50 is TRUE when Mark is 50 |
| Logical | AND, OR, NOT | Age >= 11 AND Age <= 18 is TRUE for 15 |
Library routines
A library routine is a ready-made routine supplied with the language that a program can call instead of writing the code itself. Using one saves time, is already tested, and makes the program shorter and easier to read. 2210 expects you to recognise and use the ones in section 4 of the syllabus.
LENGTH(String) returns the number of characters, so LENGTH("Happy Days") is 10 and a length check can be written as LENGTH(Password) >= 8. SUBSTRING(String, Start, Length) returns part of a string, so SUBSTRING("Happy Days", 1, 5) is "Happy"; the start position is normally 1 for the first character. UCASE and LCASE convert to upper or lower case, which is how a program can compare an answer without caring about capitals.
ROUND(Value, Places) rounds a REAL to a given number of decimal places, which is what turns 3.666666 into 3.67. RANDOM() returns a number between 0 and 1 inclusive, and is turned into a whole number in a range by multiplying and rounding, as in ROUND(RANDOM() * 6, 0). MOD and DIV also appear in this list because they are supplied operations rather than symbols.
| Routine | What it returns | Example |
|---|---|---|
| LENGTH | The number of characters in a string | LENGTH("Happy Days") returns 10 |
| SUBSTRING | A piece of a string, from a start position for a given length | SUBSTRING("Happy Days", 1, 5) returns "Happy" |
| UCASE | The string or character in upper case | UCASE("Happy") returns "HAPPY" |
| LCASE | The string or character in lower case | LCASE('W') returns 'w' |
| ROUND | The value rounded to a number of decimal places | ROUND(3.666666, 2) returns 3.67 |
| RANDOM | A random number from 0 to 1 inclusive | ROUND(RANDOM() * 6, 0) returns a whole number from 0 to 6 |
Creating a maintainable program
A program is maintainable when someone other than its author can read it, find the part they need and change it without breaking something else. That someone is often the same programmer six months later, and it is what most of the marks in a maintainability question are about.
The first habit is meaningful identifiers. TotalScore says what it holds; T, X1 and Temp2 do not. Identifiers should describe the value rather than its type, so PupilCount is better than Integer1, and a procedure should be named for what it does, such as CalculateAverage.
The second habit is commenting. In 2210 a comment begins with // and runs to the end of the line. Comments should explain why something is done, or what a procedure is for, rather than repeating the code line by line. Each procedure and function is normally preceded by a comment describing its purpose and its parameters.
The third habit is structure. Procedures and functions break a long program into named parts that can be tested on their own, constants remove unexplained numbers, and consistent indentation shows which statements sit inside which loop or IF. A program arranged like this can be changed in one place instead of twenty, which is the whole point of maintainability.
- Name variables, constants and subroutines for what they mean, not for how short the name is.
- Comment each subroutine with its purpose, and comment anything whose reason is not obvious.
- Use constants for fixed values and procedures for repeated blocks of code.
Key terms you must be able to define
- Variable
- A named store whose value can change while the program is running. It is declared with a data type before it is used.
- Constant
- A named store whose value is fixed when the program is written and cannot be changed while it runs.
- Data type
- The kind of value a variable may hold, which decides what can be stored in it and which operations are allowed.
- INTEGER
- A data type holding a whole number, positive, negative or zero.
- REAL
- A data type holding a number with a fractional part, written with a decimal point.
- CHAR
- A data type holding a single character, written in single quotes.
- STRING
- A data type holding zero or more characters, written in double quotes.
- BOOLEAN
- A data type holding only TRUE or FALSE, which is the result of every comparison.
- Sequence
- Statements carried out one after another, in the order in which they are written.
- Selection
- A construct that chooses between paths, written as an IF statement or a CASE statement.
- Iteration
- A construct that repeats statements, written as a count-controlled, pre-condition or post-condition loop.
- Count-controlled loop
- A FOR ... NEXT loop that repeats a known number of times, using an INTEGER counter.
- Pre-condition loop
- A WHILE ... DO ... ENDWHILE loop that tests its condition before the statements run, so it may run zero times.
- Post-condition loop
- A REPEAT ... UNTIL loop that tests its condition after the statements run, so it always runs at least once.
- Nested statement
- A statement written inside another statement, such as an IF inside an IF or a loop inside a loop, shown by deeper indentation.
- DIV
- An integer operation returning the whole-number quotient of one integer divided by another, discarding the remainder.
- MOD
- An integer operation returning the remainder left when one integer is divided by another.
- Library routine
- A ready-made routine supplied with the programming language that a program can call instead of writing the code itself.
- Maintainable program
- A program written so that another programmer can read it, find the part they need and change it without breaking the rest.
In context
2210 sets its questions inside systems a student already uses - a school network, a phone, a supermarket till, a vending machine, a website. These show how the theory is applied to a context you have not seen before, then you answer for the system you are given.
The price of a meal is held as a REAL because it has pence, the number of items is an INTEGER, and the name of the meal is a STRING. The total is set to 0 before the loop that scans the items, and each price is added with Total ← Total + Price, so the customer pays the sum rather than the last price scanned.
The battery level is tested with selection: IF BatteryLevel < 20 THEN the phone shows a warning and dims the screen, ELSE it carries on normally. The test is repeated inside a loop, because the level has to be checked again and again while the phone is in use, and a WHILE loop is used because the check must happen before anything is displayed.
The menu is shown with REPEAT, because it must appear at least once before the player can choose to quit. The choice is read into a CHAR and passed to a CASE statement with cases for 'P', 'S' and 'Q' and an OTHERWISE branch that prints an error, so a key that is not on the menu produces a message instead of crashing the game.
A score is kept as an INTEGER and a flag called AnsweredCorrectly as a BOOLEAN. The answer is compared with LCASE so that the player can type the answer in capitals or not, and a FOR loop runs the ten questions because the number of questions is fixed. The final score is printed with an OUTPUT statement after the loop, not inside it.
The maximum number of books a member may borrow is stored as a constant, because it is the same for every member and changes only once a year. The member's name is a STRING, the number of books borrowed is an INTEGER, and the program is split into procedures for adding a loan and for returning a book, each with a comment above it saying what it does.
Flashcards
Click a card to flip it. Mark honestly — the tracker uses this.
Exam-style questions
7 questions, 24 marks. Write your answer and press Mark my answer — it is marked against the scheme and tells you which points you made and what would gain the next mark. You can still reveal the scheme and mark it yourself instead.
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Mark scheme — tick each point you actually made
Exam technique for this topic
- Use ← for assignment and = only for comparison. A line such as Total = Total + 1 is a comparison in Cambridge pseudocode, not an instruction to add anything.
- Close every construct: ENDIF for each IF, ENDCASE for each CASE, NEXT for each FOR, ENDWHILE for each WHILE and UNTIL for each REPEAT. A missing terminator loses marks even when the logic is right.
- Indent by two spaces inside a THEN, an ELSE or a CASE, and by four spaces for statements inside a loop, so that the structure of the pseudocode is visible to the examiner.
- Choose the loop to fit the problem and say why: FOR when the number of repetitions is known, WHILE when it might be zero, REPEAT when it must happen at least once.
- In a maintainability question, give a named example. 'Meaningful identifiers such as PupilCount rather than X' earns the mark; 'good variable names' does not.
Why students lose marks here
Where this fits in O Level Computer Science 2210
This page is one topic of Cambridge O Level Computer Science 2210. O Level Computer Science 2210 is the course home: every area and every topic in Cambridge's own order, the papers you sit and what each one tests, and the exam-practice tools. The full topic list, with each topic linked to its revision page, is on the O Level Computer Science 2210 syllabus page.