What you'll learn
Programming is where every other part of this course becomes something you can actually run. This topic covers the raw materials — data types, variables and constants — the three structures that control the order things happen in, the operators that do the work, the data structures that hold more than one value, the subroutines that keep a large program manageable, and the testing that decides whether any of it is right. By the end of this guide you should be able to trace a loop by hand, evaluate expressions involving DIV, MOD and Boolean operators, use string-handling functions, explain variable scope, choose appropriate test data, and tell a syntax error from a logic error.
Key terms and definitions
Variable — A named storage location whose value can change while the program runs.
Constant — A named value that cannot change while the program runs.
Data type — The kind of value a variable holds: integer, real, Boolean, character or string.
Sequence — Instructions carried out one after another in the order written.
Selection — Choosing between paths depending on whether a condition is true.
Iteration — Repeating a block of code.
Array — A data structure holding multiple values that must all be of the same data type.
Record — A collection of related fields, which may be of different data types, describing one thing.
Subroutine — A named block of code that performs a task and can be called from elsewhere.
Parameter — A value passed into a subroutine when it is called.
Local variable — A variable usable only inside the subroutine where it was declared.
Validation — Checking that entered data is sensible and in the correct format.
Authentication — Confirming that a user is who they claim to be.
Core concepts
Data types
Choosing the type decides what can be stored and what operations make sense. An integer is a whole number; a real has a fractional part and so suits a price in pounds and pence; a Boolean stores only true or false; a character holds one symbol and a string holds a sequence of them. An integer would silently lose the pence from a price, which is why the choice is not a formality.
Variables and constants
A variable's value changes as the program runs; a constant is fixed at the start. Using a constant for something like a VAT rate means the value appears once and is named, so changing it is a one-line edit rather than a search through the code.
The three constructs
Sequence is the default, and it is the reason the order of lines matters — swapping two statements can change the result entirely. Selection uses IF, ELSE IF and ELSE to take different paths. Iteration repeats:
- A FOR loop is count-controlled and suits a known number of repetitions — every item in a list, or exactly five times.
- A WHILE loop is condition-controlled and tests before each repetition, so it may run zero times. It suits repeating until something happens, such as the user finally entering a valid password.
Loops can be nested, and when they are, the inner loop runs in full for every single pass of the outer one. That is why the totals multiply rather than add.
Operators
Arithmetic: + − × ÷ plus two that behave differently from ordinary division:
- DIV gives integer division, discarding the remainder. 17 DIV 5 = 3.
- MOD gives the remainder. 17 MOD 5 = 2.
Together they split a number into its digits, which is a very common exam use.
Relational: =, ≠, <, >, ≤, ≥. Note that ≥ includes the boundary value itself, so a check of mark ≥ 40 accepts exactly 40.
Boolean: AND requires every operand to be true; OR requires at least one; NOT inverts. Evaluate from inside the brackets outwards — applying a NOT to the whole expression instead of the variable it belongs to is the usual source of lost marks.
Data structures
An array holds many values of one type, accessed by position. A record holds related fields that may be of different types, describing one thing — a name as a string, an age as an integer and a membership flag as a Boolean together, which is what makes it suited to one row of a database.
String handling
Strings are manipulated through functions rather than arithmetic:
- LEN returns the number of characters, including spaces and punctuation.
- SUBSTRING extracts a section. Read the indexing convention carefully: if positions count from 0 and both ends are included, SUBSTRING(2, 4, 'computer') gives 'mpu'.
- Concatenation joins strings: 'data' + 'base' gives 'database'.
- Case conversion and character-to-code functions convert between forms.
Note that the + symbol behaves differently depending on the types it is given: between two strings it concatenates, between two numbers it adds.
Subroutines, parameters and scope
A procedure carries out an action; a function returns a value to the point where it was called, which is why a function can appear inside an expression. Both can take parameters and both can be called many times.
Scope decides where a variable can be used. A local variable exists only inside its own subroutine, so two subroutines can each use a counter called i without interfering. A global variable is visible throughout the program.
Prefer parameters to globals. A subroutine that receives what it needs is self-contained: it can be copied into another program unchanged, tested on its own, and cannot accidentally alter anything elsewhere. Globals create hidden links between distant parts of a program, which is what makes a change in one place break something apparently unrelated.
Structured and robust programming
Indentation shows which statements belong inside each block, which prevents the common bug of a statement sitting outside the loop meant to contain it. Comments explain intent to a human reader. Neither affects how fast the program runs.
Validation checks that entered data is possible — a date of 32 January or an empty name field is caught at the point of entry. It cannot guarantee the data is true, so a correctly formatted but wrong postcode still gets through. Authentication is a different job: it establishes identity, usually with a password, and increasingly with a second factor.
Testing and errors
Test with three kinds of data:
- Normal — ordinary values the program should accept.
- Boundary — values exactly on the edge of what is allowed, because that is where > gets written instead of ≥.
- Erroneous — values the program should reject: wrong data type, wrong format, or outside the permitted range.
A program can handle ordinary input perfectly and still fail at the limits or crash on nonsense, which is why all three are needed.
Two kinds of error matter. A syntax error breaks the rules of the language, so nothing runs and the translator names the offending line. A logic error obeys every rule and runs to completion, simply producing the wrong answer — which is why logic errors are far harder to find. Nothing complains; the fault shows only when someone checks the output against what it should have been.
Worked examples
Example 1: Tracing an accumulator loop
An algorithm sets total to 0, then FOR i = 1 TO 4 adds i to total, then outputs total.
The loop runs with i equal to 1, 2, 3 and 4, and total accumulates 1, then 3, then 6, then 10. The common slip is to answer 4, which reports the final value of the counter rather than the running total.
Example 2: Nested loops
count starts at 0. FOR i = 1 TO 3 contains FOR j = 1 TO 4, which adds 1 to count.
The inner loop runs four times for every single pass of the outer loop, so count ends at 3 × 4 = 12. Answering 7 adds the limits instead of multiplying them.
Example 3: DIV and MOD together
Evaluate (47 DIV 10) + (47 MOD 10).
47 DIV 10 is 4, the number of whole tens. 47 MOD 10 is 7, the remainder. The sum is 11. This pair is exactly how a program separates a two-digit number into its digits.
Example 4: Choosing test data
A field accepts whole numbers from 11 to 18 inclusive.
- Normal: 14, 15 — ordinary values that should be accepted.
- Boundary: 11 and 18 — exactly on the limits.
- Erroneous: the text 'twelve' (wrong type), 14.5 (not a whole number), 25 (out of range).
Note that 15 is normal rather than erroneous, and that 11 must be accepted — a test using ≥ 12 would wrongly reject it.
Common mistakes and how to avoid them
Confusing DIV and MOD is the most frequent error. DIV gives the whole-number part; MOD gives the remainder. Say which one you want before you calculate.
When tracing a loop, do not report the counter when the question asks for the accumulator. Write a small trace table with one column per variable.
With nested loops, multiply the iteration counts rather than adding them.
In Boolean expressions, work outwards from the brackets and apply each NOT only to the variable it precedes.
Do not assume a logic error causes a crash. A program with a logic error runs perfectly and gives the wrong answer, which is exactly why it is the harder kind to find.
Read the indexing convention in any SUBSTRING question. Counting from 0 and counting from 1 give different answers, and the question will tell you which applies.
Finally, keep validation and authentication apart. Validation asks whether the data is possible; authentication asks whether the user is genuine.
Exam technique for "Programming"
For trace questions, draw a trace table with one column per variable and one row per iteration. It is faster than holding values in your head and it earns method marks when the final answer slips.
When writing or completing code, state the construct you are using and indent properly — examiners credit structure, and an unindented nested loop is easy to get wrong. Use the pseudo-code conventions given in the question rather than a real language's syntax.
For questions on test data, give a specific value for each category and say what the program should do with it. "Boundary data" alone earns nothing; "11, which should be accepted" earns the mark.
When asked to explain a design choice — a parameter rather than a global, a WHILE rather than a FOR — name the benefit and the consequence. For subroutines that usually means reusability and isolation; for loops it is whether the number of repetitions is known in advance.
Quick revision summary
- Types: integer (whole), real (fractional), Boolean (true/false), character, string. A price needs a real.
- Constructs: sequence, selection (IF/ELSE), iteration (FOR = count-controlled, WHILE = condition-controlled and may run zero times).
- Nested loops multiply: 3 × 4 = 12 iterations.
- DIV = integer division (17 DIV 5 = 3); MOD = remainder (17 MOD 5 = 2).
- AND needs all true, OR needs one, NOT inverts; evaluate outwards from brackets.
- Array = many values, one type. Record = related fields, mixed types.
- LEN counts characters; SUBSTRING extracts; + concatenates strings and adds numbers.
- A function returns a value; a procedure does not. Prefer parameters to globals for reusable, isolated code.
- Test with normal, boundary and erroneous data.
- A syntax error stops the program running; a logic error runs fine and gives the wrong answer.