Programming Fundamentals
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.
| Feature | Variable | Constant |
|---|---|---|
| Value can change at runtime | Yes | No |
| Typical naming convention | camelCase or snake_case | ALL_CAPS |
| Example uses | Score, user input, running total | Maximum 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
Trueexecutes; 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.
How much of this have you taken in?
Quiz yourself on this section — free, no card needed.
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
Falseon 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:
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Addition | 7 + 3 | 10 |
- | Subtraction | 7 - 3 | 4 |
* | Multiplication | 7 * 3 | 21 |
/ | Division | 7 / 2 | 3.5 |
MOD | Remainder (modulo) | 7 MOD 2 | 1 |
DIV | Integer quotient | 7 DIV 2 | 3 |
^ | Exponentiation | 2 ^ 8 | 256 |
Comparison operators — produce a Boolean result:
== (equal), != (not equal), <, <=, >, >=
Boolean operators — combine Boolean expressions:
| Operator | Result is True when... | Example |
|---|---|---|
AND | Both conditions are True | age >= 18 AND hasID == True |
OR | At least one condition is True | grade == "A" OR grade == "B" |
NOT | The condition is False | NOT gameOver |
MODandDIVare 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).
| Mistake | Correction |
|---|---|
if score = 50 | if score == 50 |
| "MOD gives the whole number part" | MOD gives the remainder; DIV gives the whole quotient |
| WHILE loop that never ends | Ensure the variable in the condition is updated inside the loop |
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
Sorting Algorithms
Data Types and Casting
Related lessons
7 Slides
7 Slides
7 Slides