Track progress, take quizzes and save notes on this lesson.

Free forever · no card needed

Start free
Intermediate

Programming Fundamentals

2.2.1 Programming fundamentals

Aligned to the OCR J277 specification

Level
Intermediate
Reading time
6 min
Published
11 June 2026
Updated
1 July 2026
On this page
  1. 1.Variables, Constants and Assignment
  2. 2.Inputs, Outputs and Sequence
  3. 3.Selection — Conditional Branching
  4. 4.Iteration — Count-Controlled Loops
  5. 5.Iteration — Condition-Controlled Loops
  6. 6.Operators: Arithmetic, Comparison and Boolean
  7. 7.Common Exam Mistakes

Key takeaways

  • The three programming constructs are sequence (instructions run in order), selection (IF/ELIF/ELSE branches on a condition), and iteration (FOR count-controlled loops and WHILE condition-controlled loops).
  • A variable's value can change at runtime; a constant is set once and never changes, making programs easier to maintain if the value needs updating.
  • MOD gives the remainder of division (17 MOD 5 = 2); DIV gives the whole-number quotient (17 DIV 5 = 3).
  • A WHILE loop checks its condition before each iteration; if the condition is False on the first check the loop body runs zero times. The variable in the condition must be updated inside the loop or an infinite loop results.
  • A common mistake: = is assignment (stores a value); == is comparison (checks equality). Using = inside an IF condition is a syntax error in most languages.

Variables, Constants and Assignment

A variable is a named storage location whose value can change while the program runs. A constant is a named value that is set once and never changes during execution.

score = 0          # variable — changes as the game progresses
MAX_SCORE = 100    # constant — fixed upper limit

An assignment statement stores a value in a variable. The variable name goes on the left; the value or expression goes on the right.

x = 5
x = x + 1      # x is now 6 — the old value of x is read, 1 is added, result stored back
name = "Alice"

Constants make programs easier to maintain — if the value needs to change (e.g. changing the maximum score from 100 to 50), you update it in one place rather than hunting through the code.

FeatureVariableConstant
Value can change at runtimeYesNo
Typical naming conventioncamelCase or snake_caseALL_CAPS
Example usesScore, user input, running totalMaximum lives, tax rate, screen size

Inputs, Outputs and Sequence

Sequence is the most basic programming construct: instructions are executed one after another, in the order they are written.

Input reads data from the user (keyboard, file, sensor). Output displays or returns data.

name = input("Enter your name: ")   # reads a string from the keyboard
age = int(input("Enter your age: ")) # reads and converts to integer
print("Hello, " + name)             # outputs a string
print("You are", age, "years old")  # outputs name and age

Worked example — program that calculates the area of a rectangle in sequence:

length = float(input("Enter length: "))
width = float(input("Enter width: "))
area = length * width
print("Area =", area)

Each line executes in order: read length, read width, calculate, display. Remove or reorder any line and the program breaks. Sequence is the foundation on which selection and iteration are built.

Selection — Conditional Branching

Selection lets a program choose between different paths depending on a condition. The condition evaluates to either True or False.

IF … ELSE is the core selection structure:

score = int(input("Enter score: "))
if score >= 50:
    print("Pass")
else:
    print("Fail")

IF … ELIF … ELSE handles multiple branches:

if score >= 70:
    grade = "A"
elif score >= 50:
    grade = "B"
else:
    grade = "C"
print("Grade:", grade)

Conditions are checked top to bottom. The first branch whose condition is True executes; all others are skipped.

In pseudocode (OCR style):

IF score >= 70 THEN
    grade = "A"
ELSE IF score >= 50 THEN
    grade = "B"
ELSE
    grade = "C"
END IF

Iteration — Count-Controlled Loops

Iteration (looping) repeats a block of code. A count-controlled loop runs a fixed number of times, determined by a counter variable.

for i in range(1, 6):     # i takes values 1, 2, 3, 4, 5
    print(i)

In pseudocode:

FOR i = 1 TO 5
    OUTPUT i
END FOR

Worked example — print the 7 times table:

for i in range(1, 13):
    print(7, "×", i, "=", 7 * i)

Output: 7 × 1 = 7, 7 × 2 = 14, … 7 × 12 = 84.

Count-controlled loops are used when the number of repetitions is known before the loop starts. Use them for: processing every item in a list, repeating a fixed number of times, building tables.

range(1, 6) generates values 1, 2, 3, 4, 5 — the upper bound is excluded. range(0, 5) gives 0, 1, 2, 3, 4.

Worth saving these ideas?

Turn what you've read into instant revision cards. Free to get started.

Make flashcards

Iteration — Condition-Controlled Loops

A condition-controlled loop repeats while (or until) a condition is true. The number of repetitions is not known in advance.

WHILE loop — checks the condition before each iteration:

password = ""
while password != "secret":
    password = input("Enter password: ")
print("Access granted")

In pseudocode:

password = ""
WHILE password != "secret"
    password = INPUT("Enter password: ")
END WHILE
OUTPUT "Access granted"

Worked example — sum numbers until the user enters 0:

total = 0
number = int(input("Enter number (0 to stop): "))
while number != 0:
    total = total + number
    number = int(input("Enter number (0 to stop): "))
print("Total:", total)

If the condition is False on the first check, a WHILE loop body executes zero times. Use a WHILE loop when the number of repetitions depends on user input or data values.

Operators: Arithmetic, Comparison and Boolean

OCR J277 requires knowledge of three categories of operator.

Arithmetic operators — perform calculations:

OperatorMeaningExampleResult
+Addition7 + 310
-Subtraction7 - 34
*Multiplication7 * 321
/Division7 / 23.5
MODRemainder (modulo)7 MOD 21
DIVInteger quotient7 DIV 23
^Exponentiation2 ^ 8256

Comparison operators — produce a Boolean result:

== (equal), != (not equal), <, <=, >, >=

Boolean operators — combine Boolean expressions:

OperatorResult is True when...Example
ANDBoth conditions are Trueage >= 18 AND hasID == True
ORAt least one condition is Truegrade == "A" OR grade == "B"
NOTThe condition is FalseNOT gameOver

MOD and DIV are commonly tested. 17 MOD 5 = 2 (remainder); 17 DIV 5 = 3 (whole quotient).

Common Exam Mistakes

1. Using = instead of == in conditions

= is assignment: x = 5 stores 5 in x.
== is comparison: x == 5 checks whether x equals 5.
Using = in an IF condition is a syntax error in most languages.

2. Confusing MOD and DIV

MOD gives the remainder: 17 MOD 5 = 2 (17 = 3×5 + 2).
DIV gives the integer quotient: 17 DIV 5 = 3 (the whole number part of 17÷5).

3. Off-by-one errors in FOR loops

FOR i = 1 TO 5 runs with i = 1, 2, 3, 4, 5 (five iterations). In Python, range(1, 5) gives 1, 2, 3, 4 (four iterations). Match the loop bounds carefully to the required repetition count.

4. WHILE loop condition never becoming False

A condition-controlled loop must have a route to termination. If the variable tested in the condition is never changed inside the loop, the loop runs forever (infinite loop).

MistakeCorrection
if score = 50if score == 50
"MOD gives the whole number part"MOD gives the remainder; DIV gives the whole quotient
WHILE loop that never endsEnsure the variable in the condition is updated inside the loop

Key terms

Variable
A named storage location in memory whose value can change while the program is running.
Constant
A named value that is set once before the program runs and does not change during execution, typically written in ALL_CAPS.
Assignment
An instruction that stores a value or expression result in a variable. The variable name is on the left and the value on the right, for example: x = 5.
Sequence
The programming construct where instructions execute one after another in the order they are written.
Selection
The programming construct that chooses between different execution paths based on whether a condition evaluates to True or False, using IF/ELIF/ELSE.
Iteration
The programming construct that repeats a block of code, either a fixed number of times (count-controlled) or while a condition holds (condition-controlled).
FOR loop
A count-controlled loop that runs a set number of times, determined by a counter variable that steps through a defined range.
WHILE loop
A condition-controlled loop that repeats while a condition is True. The condition is checked before each iteration.
MOD
An arithmetic operator that returns the remainder of integer division, for example 17 MOD 5 = 2.
DIV
An arithmetic operator that returns the whole-number quotient of integer division, for example 17 DIV 5 = 3.
Boolean operator
An operator (AND, OR, NOT) that combines or negates Boolean expressions to produce a True or False result.

Frequently asked questions

A count-controlled loop (FOR) runs a fixed number of times determined before it starts. A condition-controlled loop (WHILE) repeats while a condition remains True, so the number of repetitions is not known in advance and depends on data or user input.

MOD gives the remainder after integer division: 17 MOD 5 = 2. DIV gives the whole-number quotient: 17 DIV 5 = 3. A common mistake is mixing them up - MOD does not give the whole number part.

A variable is a named storage location whose value can change while the program runs. A constant is named once and its value never changes during execution. Constants are typically written in ALL_CAPS and make programs easier to maintain.

Generate revision on any topic you study

Type any topic you're studying and Aicademy generates a complete lesson, quiz, and flashcard set, personalised to your level.

Lessons on anything

Structured, level-matched lessons on any topic you study

Practice quizzes

Find out what you actually know before the exam does

Flashcard sets

Lock in key concepts with instant revision cards

Ask Aica

Stuck on something? Get a clear explanation, any time

Prev

Sorting Algorithms

Next

Data Types and Casting

Related lessons

7 min

7 min

7 min

Lesson

Defensive Design

OCR GCSE Computer Science · OCR J277

1 month ago

Top students don’t revise more. They revise what counts.

Start revising free

Free to start. No card needed.