An array stores several values under one name, and each value is reached by its index. Loops and arrays go together: one loop visits every index, so the loop bounds decide whether you visit every value exactly once.
This lesson starts advanced programming practice. If variables and data types still feel shaky, revisit variables, constants and data types first.
How do I read an array?
Draw it as a row of boxes with the index above and the value inside. Do this before you read any code, because most array mistakes are index mistakes.
Take an array called marks with five values, indexed from 0.
| Index | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| marks | 72 | 55 | 90 | 64 | 81 |
marks[2] is 90, because the index counts from 0. The array has 5 elements, so the last index is 4, not 5.
Worked example 1: total and average
Write the steps to find the total and average of the marks.
total = 0
FOR i = 0 TO 4
total = total + marks[i]
NEXT i
average = total / 5
Trace it: 72, then 127, then 217, then 281, then 362. The total is 362, so the average is 362 ÷ 5 = 72.4.
The loop runs from 0 to 4, which is five steps, and the division also uses 5. Both come from the same fact: the array has five elements.
The mistake that misses or overshoots
A common slip is to loop up to the number of elements instead of the last index. It feels natural because the count is the number you know.
| Loop | Indexes visited | Result |
|---|---|---|
| FOR i = 0 TO 5 | 0, 1, 2, 3, 4, 5 | marks[5] does not exist, so a runtime error or a garbage value |
| FOR i = 1 TO 4 | 1, 2, 3, 4 | marks[0] = 72 is skipped, so the total is 290 |
| FOR i = 0 TO 4 | 0, 1, 2, 3, 4 | Correct: 362 |
Both wrong loops look fine on a quick read. A trace table with an index column shows the skipped or extra step at once.
Worked example 2: structured data
Suppose each student has a name and a mark. A record groups them.
| Record | name | mark |
|---|---|---|
| student[0] | Aina | 72 |
| student[1] | Ben | 55 |
| student[2] | Chong | 90 |
To print the names of students with a mark of 60 or more:
FOR i = 0 TO 2
IF student[i].mark >= 60 THEN
PRINT student[i].name
ENDIF
NEXT i
The output is Aina, then Chong. A record keeps student[i].name and student[i].mark together for the same person. Two separate arrays can fall out of step, for example if one is sorted and the other is not.
A routine for any array question
- Write the index row and the value row.
- Write the number of elements and the last valid index.
- Copy the loop bounds beside them and confirm they match.
- Trace the first step and the last step before you trace the middle.
Check yourself
An array temps holds 28, 31, 30, 27 at indexes 0 to 3. What does this code print, and what is wrong if the loop were FOR i = 0 TO 4?
hot = 0
FOR i = 0 TO 3
IF temps[i] > 29 THEN
hot = hot + 1
ENDIF
NEXT i
PRINT hot
Answer
Trace: i = 0 gives 28, not above 29, so hot stays 0. i = 1 gives 31, so hot is 1.
Then i = 2 gives 30, so hot is 2. Finally i = 3 gives 27, so hot stays 2. The code prints 2.
With FOR i = 0 TO 4, the loop would read temps[4], which does not exist for an array with 4 elements. Depending on the language, that is a runtime error or an invalid value. The last index of a 4-element array is 3.
What to study next
Next, use arrays inside algorithms: tracing searching and sorting algorithms within scope. You can also practise following code line by line with the restricted pseudocode trace trainer.
If you want a teacher to trace your own array code with you, see online one-to-one Computer Science tuition.