Programming
Writing programs using variables, data types, selection, loops, string handling, subroutines, arrays and files, in the Cambridge pseudocode style.
📌 Key points
- A variable stores a value that can change while the program runs; a constant stores a value that does not change (e.g. CONSTANT Pi ← 3.142); identifiers should be meaningful. Assignment uses ←.
- Data types: INTEGER (whole number), REAL (number with a decimal part), CHAR (single character), STRING (text) and BOOLEAN (TRUE or FALSE).
- The three basic constructs are sequence, selection (IF … THEN … ELSE … ENDIF, and CASE OF … OTHERWISE … ENDCASE) and iteration (loops).
- Loops: FOR … TO … NEXT is count-controlled (fixed number of repeats); REPEAT … UNTIL is post-condition (always runs at least once); WHILE … DO … ENDWHILE is pre-condition (may not run at all).
- Arithmetic: + − * / plus MOD (remainder, 17 MOD 5 = 2) and DIV (whole-number division, 17 DIV 5 = 3); ROUND(Value, Places) rounds and RANDOM() gives a random number between 0 and 1.
- String handling: LENGTH(S) gives the number of characters, SUBSTRING(S, Start, Length) extracts part of a string, UCASE and LCASE change to upper or lower case.
- Procedures and functions are named subroutines that can take parameters; a function returns a value. Local variables exist only inside their subroutine; global variables can be used anywhere in the program.
- An array stores many values of the same data type under one identifier, using an index: DECLARE Scores : ARRAY[1:30] OF INTEGER; a 2D array has rows and columns, e.g. ARRAY[1:3, 1:4]; arrays are often processed with FOR loops.
- Files store data permanently: OPENFILE "Data.txt" FOR READ (or WRITE), READFILE and WRITEFILE to read or write a line, then CLOSEFILE.
- Maintainable programs use meaningful identifiers, comments (// …), indentation, and procedures and functions so the code is easy to understand and update.
📖 Key terms
- Variable
- A named memory location that stores a value that can change while the program runs.
- Constant
- A named value that cannot change while the program runs.
- Count-controlled loop
- A loop that repeats a set number of times (FOR … NEXT).
- Post-condition loop
- A loop whose condition is checked at the end, so it runs at least once (REPEAT … UNTIL).
- Function
- A subroutine that returns a single value to the code that called it.
- Parameter
- A value passed into a procedure or function when it is called.
- Local variable
- A variable declared in a subroutine that can only be used in that subroutine.
- Array
- A data structure holding several items of the same data type under one identifier, accessed by index.
✏️ Worked example
Write pseudocode to input 10 numbers and output their total and average.
- Set the total to zero before the loop: Total ← 0
- Use a count-controlled loop: FOR Count ← 1 TO 10
- Inside the loop input each number and add it to the total: INPUT Number then Total ← Total + Number
- Close the loop: NEXT Count
- After the loop: Average ← Total / 10
- Output both: OUTPUT Total, Average
In pseudocode answers, initialise totals and counters before the loop and output after the loop. Putting OUTPUT inside the loop, or Total ← 0 inside it, are very common mistakes.
Test yourself8 questions from a bank of 17. A new mix every time.
Start the quiz →