Algorithms and programming · GCSE Computer Science

Programming basics

Sequence, selection, iteration, data types, strings, arrays, functions, validation and the three faces of errors.

UNDERSTANDRETRIEVEREMEMBER
THE MEMORY HOOK
Computers do exactly what you wrote — which is why the bug is yours.

The important bits

What you need to know

  1. 1

    Every program is built from sequence (one statement after another), selection (IF / ELIF / ELSE, CASE) and iteration (FOR counted loops, WHILE / REPEAT condition loops). If you can name which construct a problem needs, you can design it.

  2. 2

    Variables bind names to values that can change; constants should not change at run time. Data types matter: integer, real/float, Boolean, character, string. Adding a string to an integer is a type error waiting to happen — cast explicitly.

  3. 3

    String handling: length, substring, concatenation, upper/lower, converting to character codes. Off-by-one errors live here because some languages index from 0.

  4. 4

    Arrays (or lists) store many items of one type under one name. You iterate with an index or a FOR EACH. Two-dimensional arrays are tables: rows then columns. Bounds checks belong in your mental model.

  5. 5

    Procedures and functions package reuse. Functions return a value; procedures (in some board languages) do not. Parameters pass data in; local variables stay inside. Global variables are convenient and dangerous.

  6. 6

    Validation checks input is reasonable (range, length, type, presence, format). Verification checks it matches the original (double entry, checksum). Validation cannot prove the user typed the true exam mark — only that it looks like a mark.

  7. 7

    Errors: syntax (the translator rejects the grammar), runtime (division by zero, missing file), logic (the program runs and is wrong). Testing uses normal, boundary and invalid data. A test plan with expected outputs is a mark-scheme favourite.

  8. 8

    Readability is not cosmetics: identifiers, indentation, comments, and avoiding magic numbers make logic errors findable. Examiners award this under “maintainability”.

Go deeper

Selection versus iteration: pick the tool

Selection chooses a path once: if age < 18, refuse the ticket. Iteration repeats until a condition fails or a count completes: keep asking for a PIN while it is wrong, or FOR i FROM 0 TO n-1 totalling an array. Nested IF is not a loop; a WHILE that never changes its condition is an infinite loop. When a question describes “keep going until the user types QUIT”, that is WHILE, not FOR, because you do not know the count in advance. When it says “do this for every student in the file”, that is a counted or FOR EACH loop. Naming the construct in a 2-mark design question is free credit if you have rehearsed the verbs: until, while, for each, if.

Go deeper

Logic errors are the ones unit tests were born for

Syntax errors are noisy; the translator points at the line. Logic errors are polite and wrong: a condition written >= where > was needed, an accumulator not reset, a loop that runs to LENGTH instead of LENGTH-1. Boundary tests (the 18-year-old on an age gate, the last index in an array) catch a huge share of them. Write the expected output before you run the code, otherwise you will talk yourself into whatever appeared. In written papers, they will show you a ten-line algorithm with one bad comparison — your job is to dry-run, not to “spot” by vibe.

WORKED EXAMPLE

See the idea in action

A program should accept integers 1–5 as menu choices. Validation: presence check, type check (integer), range check 1 to 5, then a WHILE that re-prompts on failure. Test plan: 3 (normal), 1 and 5 (boundary), 0 and 6 (invalid range), “three” (invalid type), blank (presence). Expected: only 1–5 proceed; others repeat the prompt. That table is the answer, not a paragraph about “making sure it works”.

Exam technique

Turn knowledge into marks

When they ask for an algorithm, use the board’s keywords (RECEIVE, SEND, REPEAT…UNTIL). Fancy Python that the paper does not recognise can still lose syntax-style marks.

Common mistakes

Do not give these marks away

  1. 01

    Confusing validation (reasonable data) with verification (matches the source).

  2. 02

    Off-by-one loops that miss the last array item or overrun it.

  3. 03

    Calling every mistake a syntax error, including programs that run and print the wrong total.

QUICK RETRIEVAL

A program runs but always undercounts the last item in a list. What type of error is this?

AA syntax error

BA logic error

CA hardware fault in the ALU

DA validation error in the operating system

Show the answer

A logic error. The program is grammatically fine and does not crash; the algorithm is wrong (often an off-by-one). Syntax would refuse to run; a runtime error would halt on an illegal operation.

Quick questions

If this is the bit you searched

What is the difference between a FOR loop and a WHILE loop?

FOR is counted: you know how many times, or you iterate a known collection. WHILE is condition-led: it may run zero times, and you must make sure the condition can become false.

Why do examiners care about constants?

A named VAT rate or board size, written once, is less error-prone than magic numbers sprinkled through the code. It is a maintainability point as much as a syntax point.