Intermediate

Programming Fundamentals

AicademyAicademy
·OCR GCSE Computer Science·OCR J277·6 min
2.2.1 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.

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.

How much of this have you taken in?

Quiz yourself on this section — free, no card needed.

Test myself

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

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 Slides

Lesson

Data Types and Casting

OCR GCSE Computer Science · OCR J277

2 days ago

7 Slides

Lesson

Subroutines and Scope

OCR GCSE Computer Science · OCR J277

2 days ago

7 Slides

Lesson

Defensive Design

OCR GCSE Computer Science · OCR J277

2 days ago