Programming Fundamentals

J277 · 2.2 Programming Fundamentals · GCSE Computer Science

Component 02

Data Types

Integer

Whole numbers, positive or negative. No decimal point.

e.g. 42, -7, 0

Real / Float

Numbers with a decimal point.

e.g. 3.14, -0.5

Boolean

Only two values — true or false.

e.g. True, False

Character

A single letter, digit, or symbol.

e.g. 'A', '3', '!'

String

A sequence of characters.

e.g. "Hello", "J277"

Variable vs Constant

Variable: value can change. Constant: fixed throughout the program.

Operators (OCR Pseudocode)

Arithmetic

+ - *Add, Subtract, Multiply
/Divide (real result)
DIVInteger division (floor)
MODRemainder after division
^Power / exponent

Comparison

==Equal to
!=Not equal to
< >Less / Greater than
<= >=Less/Greater or equal

Logical

ANDBoth must be true
ORAt least one true
NOTInverts true/false

Control Structures (OCR Pseudocode)

Selection

IF score >= 70 THEN
  grade = "Distinction"
ELSEIF score >= 50 THEN
  grade = "Pass"
ELSE
  grade = "Fail"
ENDIF

FOR Loop

total = 0
FOR i = 0 TO 9
  total = total + scores[i]
NEXT i
average = total / 10

WHILE Loop

attempts = 0
WHILE attempts < 3
  password = input("Enter:")
  attempts = attempts + 1
ENDWHILE

Arrays

An array stores multiple values of the same type in a single variable. Each value is accessed by its index, starting at 0.

# 1D Array
scores = [45, 72, 61, 88, 55]
print(scores[0])  # outputs 45
print(scores[3])  # outputs 88

# Loop through array
FOR i = 0 TO 4
  print(scores[i])
NEXT i
# 2D Array (grid)
grid = [[1,2,3],[4,5,6],[7,8,9]]
print(grid[1][2])  # outputs 6
# grid[row][column]

# Loop through 2D array
FOR row = 0 TO 2
  FOR col = 0 TO 2
    print(grid[row][col])
  NEXT col
NEXT row

Basic SQL

SQL (Structured Query Language) is used to retrieve data from a database. At GCSE you need SELECT, FROM, WHERE.

-- Select all students with score above 60
SELECT name, score
FROM Students
WHERE score > 60

-- Select all fields from a table
SELECT *
FROM Students
WHERE year = 11

Code Tracer

Step through the program below. Enter the variable values at each step, then click Check.

total = 0
FOR i = 1 TO 4
  total = total + i
NEXT i
print(total)
StepLineitotal

Spot the Bug

Each snippet has one bug. Click to reveal what's wrong.