Learn · GCSE Computer Science · Component 2
OCR-GCSE-CS-PROGRAMMING · Programming fundamentals

Programming fundamentals.

Written for OCR J277 Official specification ↗ Updated 2026.07.06

HookTwo operators turn 250 into '4 hours and 10 minutes'

A stopwatch reports 250 minutes and you want it shown as '4 hours and 10 minutes'. The whole conversion is two operators. 250 DIV 60 gives 4 — integer division throws away the remainder and keeps the whole-number quotient. 250 MOD 60 gives 10 — the modulus operator keeps only the remainder. Two lines, and a raw number becomes something a human can read. That is programming in miniature: small, exact building blocks combined to do useful work.

That one example already leans on almost every idea in this section — a variable holding the input, an assignment putting a result somewhere, arithmetic operators, and an integer data type behaving differently from a decimal one. Programming fundamentals is the small, fixed toolkit that every program in the world is built from: the three ways of controlling what happens next, the operators that do the work, the data types that decide how values behave, and the techniques — arrays, subprograms, files, SQL — that let you scale from three lines to three thousand. Master the toolkit and the language you happen to use becomes a detail.

ModelVariables, operators and the three constructs

A variable is a named box in memory whose value can change as the program runs; a constant is a named value that is fixed once set, such as VAT = 0.20. Assignment puts a value into a variable, written score = 0, and it is not the same as the comparison score == 0, which asks a true-or-false question. That single-equals-versus-double-equals distinction is one of the most common sources of confusion for beginners.

Every program, however large, is built from just three constructs. Sequence is running statements one after another in order. Selection chooses between paths using IF, ELSE IF and ELSE, or a SWITCH/CASE. Iteration repeats statements: a count-controlled loop (FOR) runs a known number of times, while a condition-controlled loop (WHILE, or DO...UNTIL) runs until some condition is met — useful when you do not know in advance how many repetitions you need, such as re-asking for a password.

The work inside those constructs is done by operators. Arithmetic operators are + , - , * , / , plus the exponent operator, and two that catch people out: DIV (whole-number division) and MOD (the remainder). Comparison operators — == , != , < , > , <= , >= — return a Boolean, and the Boolean operators AND, OR and NOT combine those results, letting a single IF test several conditions at once.

Worked example

This algorithm adds up the even numbers from 1 to 10, combining iteration, selection and three operators:

total = 0 FOR i = 1 TO 10 IF i MOD 2 == 0 THEN total = total + i ENDIF NEXT i PRINT total

The FOR loop is count-controlled. Each pass, i MOD 2 == 0 asks 'is the remainder when i is divided by 2 zero?' — true only for even numbers. When i is 2, 4, 6, 8 and 10 the condition is true and that value is added on. Trace the total: 2, then 6, then 12, then 20, then 30. The program prints 30. Change the test to i MOD 2 == 1 and the same structure sums the odd numbers to 25 instead — one operator, a completely different result.

ModelData types and casting

Every value has a data type that tells the computer how to store it and what operations make sense. OCR expects five: integer (a whole number, such as 42), real (a number with a decimal part, such as 3.14 — sometimes called float), Boolean (only True or False), character (a single symbol, such as 'A'), and string (a sequence of characters, such as 'hello'). Choosing the right type saves memory and prevents nonsense operations — you should never be able to multiply two names together.

The trap is that the same symbols can mean different things depending on type. With integers, 7 + 3 is 10. With strings, '7' + '3' is '73', because + on strings means join, not add. This is why casting — deliberately converting a value from one type to another — matters. Input from a keyboard almost always arrives as a string, so if you want to do arithmetic with it you must cast it first with something like int(...) or real(...); to print a number inside a message you often cast the other way with str(...).

Worked example

A program asks for a number and tries to add 3:

x = input('Enter a number') PRINT x + 3

Because input returns a string, if the user types 7 then x holds the string '7', and '7' + 3 either causes a type error or, in some languages, produces the string '73' — never 10. The fix is to cast on the way in:

x = int(input('Enter a number')) PRINT x + 3

Now x is the integer 7 and the program correctly prints 10. The same idea runs the other way when building output: PRINT 'You scored ' + str(score) casts the integer score to a string so it can be joined onto the message.

MechanismArrays and records — storing many values

A single variable holds one value; storing thirty test scores in thirty separate variables would be unworkable. An array solves this: it is an ordered collection of values, all of the same type, stored under one name and reached by a numbered index. Crucially, indexing usually starts at 0, so the first element of scores is scores[0] and the fifth is scores[4]. Arrays pair naturally with count-controlled loops: FOR i = 0 TO 29 lets you visit every element in turn.

A two-dimensional (2D) array stores a grid, addressed by two indices — typically row then column — which is ideal for a seating plan, a game board or a spreadsheet-style table. A record is different: it groups together several values of different types that describe one thing, such as a pupil's name (string), age (integer) and whether they are present (Boolean). Where an array is a list of similar items, a record is a single item with several labelled fields.

Worked example

A class register is stored as a 2D array marks, with one row per pupil and one column per test. Pupil 0's three marks sit in row 0:

marks[0] = 15, 18, 12 (row 0, columns 0 to 2) marks[1] = 20, 14, 19 (row 1) marks[2] = 11, 16, 17 (row 2)

To read pupil 2's mark in the second test you write marks[2][1], which is 16 — remember both indices count from 0, so column 1 is the second column. To total pupil 0's marks you loop across their row: FOR t = 0 TO 2 : total = total + marks[0][t] : NEXT t, giving 15 + 18 + 12 = 45.

MechanismStrings, files, random numbers and subprograms

OCR's additional techniques give programs their reach. String manipulation lets you pick a text apart: finding its length, extracting a substring, converting to upper or lower case, and joining strings by concatenation with +. File handling lets data survive after the program closes: you open a file, read from or write to it, and — importantly — close it so the changes are saved and the file is released. Random number generation produces unpredictable values for games, simulations and passwords, typically with a call like random(1, 6) to imitate a die.

The most powerful idea here is the subprogram — a named, reusable block of code, and the practical form of decomposition. A function takes in parameters and returns a value you can use in an expression; a procedure performs actions (such as printing) but returns nothing. Subprograms remove repetition, make code readable, and let each part be tested in isolation. Variables created inside a subprogram are usually local — they exist only while that subprogram runs — which keeps different parts of a program from interfering with each other.

Worked example

A program builds a username from a surname and a random number using several techniques at once:

FUNCTION makeUsername(surname) prefix = surname.substring(0, 3) number = random(100, 999) RETURN prefix.upper + str(number) ENDFUNCTION

Calling makeUsername('Okoro') takes the substring of the first three characters ('Oko'), forces it to upper case ('OKO'), generates a random number such as 482, casts it to a string, and concatenates the two. The function returns 'OKO482'. Because it is a function it hands a value back to whatever called it; a procedure version would instead PRINT the username and return nothing.

DataSQL — asking a database a question

When data lives in a database table rather than an array, you retrieve it with SQL (Structured Query Language). OCR expects the SELECT statement, built from three parts. SELECT names the fields (columns) you want, or * for all of them. FROM names the table to look in. WHERE is the optional filter that keeps only the rows matching a condition, and conditions can be combined with AND and OR just like a Boolean expression in code.

SQL is declarative: you describe what you want, not the step-by-step loop to fetch it, which is why one short statement can replace a page of searching code. The skill the exam tests is translating an English request — 'show the name and price of every audio product costing more than 20 pounds' — into the correct three clauses in the correct order.

Worked example

Given a table Products with fields Name, Category and Price, the request 'list the name and price of every audio product priced above 20' becomes:

SELECT Name, Price FROM Products WHERE Category = 'Audio' AND Price > 20

SELECT chooses only the two columns asked for (not the whole row), FROM points at the Products table, and WHERE keeps only rows where both conditions hold, because they are joined by AND. Swap AND for OR and the query would instead return every audio product plus every product over 20 pounds regardless of category — a much longer list, and a common exam trap.

VocabularyKey terms the mark scheme pays for

Variable and constant
A variable is a named store whose value can change while the program runs; a constant is a named value fixed once it is set, such as VAT = 0.20.
Assignment
Putting a value into a variable, written score = 0. Distinct from comparison (==), which tests whether two values are equal and returns a Boolean.
Selection
Choosing between paths of execution using IF / ELSE IF / ELSE or SWITCH/CASE, based on a condition that evaluates to True or False.
Iteration
Repeating statements. Count-controlled loops (FOR) run a known number of times; condition-controlled loops (WHILE, DO...UNTIL) run until a condition is met.
DIV and MOD
DIV gives the whole-number quotient of a division (7 DIV 2 = 3); MOD gives the remainder (7 MOD 2 = 1). Distinct from /, which can return a real number.
Data type
How a value is stored and what can be done with it: integer, real (float), Boolean, character or string. Choosing the right type saves memory and prevents invalid operations.
Casting
Deliberately converting a value from one data type to another, such as int('7') to turn keyboard input into a number, or str(5) to join a number onto a message.
Array
An ordered collection of same-type values under one name, reached by a numbered index (usually from 0). A 2D array stores a grid addressed by row and column.
Record
A structure grouping several values of different types that describe one thing, such as a pupil's name, age and attendance stored as labelled fields.
Subprogram
A named, reusable block of code. A function takes parameters and returns a value; a procedure performs actions but returns nothing. The practical form of decomposition.
SQL SELECT
A database query built from SELECT (which fields), FROM (which table) and an optional WHERE filter, whose conditions can be combined with AND and OR.

TrapsMisconceptions that cost marks

“The = sign always means 'is equal to'.”
Actually: A single = is assignment — it puts a value into a variable. Testing whether two values are equal uses the comparison operator ==. Writing = where you mean == is one of the classic beginner bugs.
“A function and a procedure are the same thing.”
Actually: A function returns a value that can be used in an expression; a procedure carries out actions (like printing) but returns nothing. Asking for the returned value of a procedure is a common exam slip.
“Dividing two numbers always gives a whole number, so / , DIV and MOD are interchangeable.”
Actually: The / operator can give a real number (7 / 2 = 3.5). DIV gives only the whole-number quotient (7 DIV 2 = 3) and MOD gives only the remainder (7 MOD 2 = 1). Picking the wrong one changes the answer.
“The first element of an array is at index 1.”
Actually: In almost all languages arrays are indexed from 0, so the first element is array[0] and an array of ten items runs from index 0 to 9. Off-by-one index errors follow directly from forgetting this.

ExamWhat examiners want

Component 2 asks you to read code (AO2) and to write and refine it (AO3), so precision with the fundamentals pays at both. When a question hands you a code fragment, treat it like an algorithm: trace the variables through each loop pass rather than guessing the output. Watch the operators especially — MOD and DIV, and single = versus == — because these are where marks are most often thrown away.

Write your answers in OCR Exam Reference Language or a consistent high-level language, and never mix the two. Declare or initialise variables before you use them, indent the body of every selection and iteration so the structure is unmistakable, and close what you open — a file-handling answer that omits the close step, or a loop with no exit condition, loses easy marks. When arithmetic on user input is involved, cast it explicitly and say why: input arrives as a string, so int(...) is the difference between adding and concatenating.

For data types, justify your choice ('a real, because the value can have a decimal part' or 'a Boolean, because there are only two states'), as the reason is usually the mark. For SQL, write the three clauses in order and quote text values in the WHERE condition; for the higher-tariff programming questions, decompose the task into subprograms, name and use your parameters, and — when asked to refine — point to the specific weakness you are fixing rather than rewriting from scratch.

Retrieve

Test yourself

Question 1 of 8

Vofti has 21 questions on OCR-GCSE-CS-PROGRAMMING — every one hook-first, every one mapped to this section of the OCR spec.

Last updated · 2026.08.09 OCR GCSE Computer Science · Spec OCR-GCSE-CS-PROGRAMMING