Programming
Variables and data types, the three constructs in pseudocode, arrays, sub-programs and file handling, the topic where the Paper 2 program is written.
Variables, constants and data types
A variable is a named memory location whose value can change while the program runs. A constant is a named value that does not change. VAT rates, π, a maximum class size. Using a constant means that when the value changes, one line is edited rather than fifteen, which is exactly the justification the paper wants.
The five data types to know: INTEGER (whole number), REAL (with a decimal part), CHAR (one character), STRING (a sequence of characters), BOOLEAN (TRUE or FALSE). Choosing the right one is marked: a phone number is a string, because it may begin with 0 and is never used in arithmetic.
In Cambridge pseudocode, ← is assignment and = is comparison. DIV gives the whole-number part of a division and MOD the remainder: 17 DIV 5 = 3, 17 MOD 5 = 2. MOD is how you test whether a number is even (N MOD 2 = 0) or find the last digit.
The three constructs in pseudocode
Selection. IF … THEN … ELSE … ENDIF for a condition; CASE OF … OTHERWISE … ENDCASE when one variable is tested against many discrete values. Conditions combine with AND, OR and NOT.
Iteration. FOR i ← 1 TO 10 … NEXT i for a known count; WHILE condition DO … ENDWHILE to test first; REPEAT … UNTIL condition to test after. A nested loop is a loop inside another, and is what processes a two-dimensional array, the outer loop moves through the rows, the inner through the columns.
Worked example · counting values over 50 in an array of 20:
Count ← 0
FOR i ← 1 TO 20
IF Scores[i] > 50 THEN
Count ← Count + 1
ENDIF
NEXT i
OUTPUT Count
Every line there is a mark: initialisation, loop, condition, increment, output.
Arrays and string handling
An array holds many elements of the same data type under one identifier, each reached by an index: Scores[3]. Declare as Scores[1:20] : INTEGER. A two-dimensional array Grid[1:3, 1:4] has 12 elements and models a table or grid.
Arrays are what make loops useful, one loop can process a thousand values. The standard operations to be able to write from memory are: total, average, find the largest or smallest, count those meeting a condition, and search for a value.
When searching, use a Boolean flag: set Found ← FALSE before the loop, set it TRUE when a match occurs, and after the loop output "not found" if it is still FALSE. Printing "not found" inside the loop prints it once per non-matching item.
String functions: LENGTH(s) gives the number of characters; SUBSTRING(s, start, count) extracts part of it; UCASE and LCASE change case. Case conversion before comparison is what makes a search work whatever the user typed.
Sub-programs, scope and files
A procedure carries out a task; a function carries out a task and returns a value. That return is the only difference the paper tests. Values are passed in as parameters.
Two reasons to use sub-programs, and questions usually want both: code written once can be reused wherever it is needed, and each sub-program can be written, tested and maintained independently, which makes a large program manageable and lets a team divide the work.
Scope. A local variable exists only inside the sub-program that declares it; a global variable is accessible everywhere. Prefer local: a global can be changed from anywhere, so a bug could originate in any part of the program, which makes debugging much harder.
Files. OPENFILE for READ to read, for WRITE to create or overwrite, for APPEND to add to the end. Opening an existing file for WRITE destroys its contents: that is a favourite exam trap. Always CLOSEFILE when finished.
Where marks go
- ← and = mixed up. Assignment versus comparison.
- Phone numbers stored as integers. Leading zeros are lost.
- Array index out of range. Check the declared bounds.
- "Not found" printed inside the loop. Use a Boolean flag and report after.
- Opening an existing file for WRITE when APPEND was meant. The data is lost.
- Function and procedure treated as identical. A function returns a value.
- Everything made global. Use local variables and parameters.
What to remember
- INTEGER, REAL, CHAR, STRING, BOOLEAN. Choose deliberately; phone numbers are strings.
- ← assigns, = compares. DIV is the quotient, MOD the remainder.
- Arrays hold one data type; nested loops process 2D arrays.
- Use a Boolean flag for "found / not found".
- Function returns a value; procedure does not. Sub-programs give reuse and easier maintenance.
- WRITE overwrites; APPEND adds. Always close the file.
Watch one worked through
Here is one done slowly, with the reason for every move. Then one for you.
A program must keep asking for a mark until the user enters one between 0 and 100. Write the validation loop and name the technique.
- Read a value once before the loop.INPUT markThe loop condition has to test something, so a value must exist before it is first checked.
- Loop while the value is out of range.WHILE mark < 0 OR mark > 100Use OR, not AND. A number cannot be both below 0 and above 100, so AND would never be true and the loop would never run.
- Inside, warn and read again.OUTPUT "Enter 0 to 100" INPUT mark ENDWHILEReading again inside the loop is what lets it end. Forgetting it gives an infinite loop, the classic bug here.
- Name it and note the boundaries.range check accepts 0 and 100, rejects −1 and 101Using < and > rather than <= and > keeps 0 and 100 valid. Test exactly on the boundary, which is where it goes wrong.
Answer: A range check: input once, then WHILE mark < 0 OR mark > 100, warn and input again
Should the condition use OR or AND? One word.
Show the answer
OR
Practise Programming
Everything on QuizByt for O Level Computer Science that goes with this lesson: read it first, then test yourself while it is fresh.
🧠 8 quizzes on Programming (5 easy, 2 medium, 1 hard)
Work up from easy to hard: the hard quiz asks the O Level exam-style questions on Programming.
🖨️ Printable worksheet
Programming on paper for O Level, with room to write the working out. Preview the first page free.
🎮 Games for O Level Computer Science
Memory and reasoning games at the right level for O Level. Passing a quiz on Programming earns the credits that open them.
Now test yourself
You have read the whole lesson. Take the quiz on this topic to see how much stuck,a free account is all it takes.
- 📊 Your score is saved, so you can see progress over time
- 🔍 See exactly which questions you got wrong, and why
- 💎 Earn credits for every quiz you pass, and spend them on games
In a hurry? Try a free Computer Science quiz with no account.