GCSE · Computer Science · AQA · Spec 8525
One-dimensional arrays
You need to store 30 test scores. Are you really going to invent 30 variable names, then type a line that adds all 30 up by hand? There's a much better way.
Watch the loop walk the array
Four scores, one loop
The code is in simple pseudocode. ← means "store this in", LEN(scores) gives the number of values in scores, and FOR … TO counts up to and including its end value.
Before you press Next: scores holds 4 values. How many times will line 4 run, and what is the last value i takes?
1 scores ← [7, 4, 9, 5]2 total ← 03 FOR i ← 0 TO LEN(scores) - 14 total ← total + scores[i]5 ENDFOR6 OUTPUT total
| i | scores[i] | total |
|---|---|---|
| 0 |
Output
Step 1: Line 1 builds the whole array in one statement: four values stored in order under one name, scores. LEN(scores) is 4, so the loop on line 3 will run from i = 0 to 4 - 1 = 3. Line 2 sets total to 0, ready for each value to be added.
Press Next one step at a time. Watch i and scores[i] change together: the index picks the slot, the slot gives the value.
Predict, then check
Commit to an answer before you look. This is the one that catches most people out.
names ← ['Ali', 'Bea', 'Cal'] names[1] ← 'Ben' What does names hold now?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
One name, a row of numbered slots, and a single loop that can visit every one of them.
What you need to know
- A data structure is a way of organising and storing related data so a program can use it efficiently.
- A one-dimensional array is one data structure: an ordered collection of elements, all the same data type, under a single identifier.
- Each element is identified by its position, called its index.
- Array name plus index in square brackets picks out one element, such as scores[2], to read it or change it.
- Arrays are commonly zero-indexed: the first element is at index 0, so n elements have indexes 0 to n - 1.
Have a gocolours ← ['red', 'blue', 'gold']. Before reading on: what does colours[1] give you?
'blue'
Counting starts at index 0, which is 'red', so index 1 is the second element. Answering 'red' means you counted positions from 1.
- You can declare an array and give it its values in one statement: names ← ['Ali', 'Bea', 'Cal'].
- Assigning to an index, like names[1] ← 'Ben', overwrites just that element. Nothing is added, and nothing else moves.
Have a goYour mate runs pets ← ['cat', 'dog', 'eel'] then pets[2] ← 'fox', and cheers: "Four pets!" How many elements does pets really hold?
Three: ['cat', 'dog', 'fox'].
Assigning to an index overwrites the element already there, so the fox replaces the eel. Nothing joins the end, so the length is still 3.
- The length is the number of elements an array holds. Accessing any index outside 0 to length - 1 is an error.
Have a goAn array holds 5 elements. Which of these indexes causes an error: 0, 4 or 5?
5
Five elements have indexes 0 to 4. 5 is tempting because it's the number of elements, but the length is one past the last index.
- A FOR loop from index 0 to length - 1 processes every element in turn: that's how totalling, finding the largest value and searching work.
- Swap many separate variables for one array and a loop can process them all, so code stays shorter and copes with any number of values.
The big picture
A one-dimensional array keeps a collection of values, all the same data type, in order, under one name. Each value is an element, and each element sits at a numbered position called its index. Write the array name and the index in square brackets, like scores[2], to read or change one element. Arrays are commonly zero-indexed, so n elements have indexes 0 to n - 1, and any index outside that range is an error. Because one name plus a changing index can reach every element, a single loop can process the whole array, however many values it holds.
Key points
Worked example
Problem
stock ← [3, 0, 8, 5]. The program then runs stock[2] ← stock[2] - 1, followed by OUTPUT LEN(stock) and then OUTPUT stock[LEN(stock) - 1]. What are the two outputs, and what does stock hold at the end?
⚠ Watch out
Counting positions from 1. It feels natural, but the first element is at index 0, so the last one is at length - 1. A loop that runs up to the length itself asks for one element too many, and that index is outside the array.
Memory hook
Picture a row of lockers with one name painted over the whole row, numbered from locker 0. The name gets you to the row; the number gets you to one locker. Four lockers means lockers 0, 1, 2 and 3, so there is no locker 4 to open.
Check yourself
data holds 6 values. What are its first and last indexes? A loop runs FOR i ← 1 TO 6 reading data[i]. Which element does it miss, and which index goes wrong?
Flashcards
(14)What is a data structure?
What is a one-dimensional array?
What is an element of an array?
What is an index?
How do you read one element of an array?
A zero-indexed array holds n elements. What are its first and last indexes?
What is the length of an array?
What happens if a program uses an index outside 0 to length - 1?
How do you create an array with its values already in it?
How do you change one element of an array?
Which loop visits every element of an array exactly once?
Why use an array instead of lots of separate variables?
Name three tasks done by looping through an array.
What must be true of the data types of an array's elements?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore AQA GCSE Computer Science topics
How this lesson was checked. This AQA GCSE Computer Science (specification 8525)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 28 October 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.