Skip to content
IGCSE·Tuition
Computer Science · Lesson

Tell an array index from its value

An array holds a row of values, and the number in the brackets is a position, not one of them.

On this page
  1. How do you read an array?
  2. Worked example
  3. The mistake to watch for
  4. Check yourself
  5. Where this leads next

An array stores several values of the same type under one name. Each value has an index, its position, and you reach it with Name[index]. The index and the value are different things, and mixing them up is a frequent source of wrong answers.

This lesson finishes the loop skills in repetition and arrays by joining them to arrays. It follows finding a maximum, where the position and the value both appeared.

How do you read an array?

An array is declared with its bounds and type:

DECLARE Level : ARRAY[1:5] OF INTEGER

Suppose it holds these values.

Index12345
Level[index]35254

Level[3] is 2. The index is 3, the value is 2. A value can itself be used as an index.

Level[Level[2]] means: first work out Level[2], which is 5, then find Level[5], which is 4.

Worked example

This algorithm finds where the largest value is kept.

Pos ← 1
FOR i ← 2 TO 5
   IF Level[i] > Level[Pos]
      THEN
         Pos ← i
   ENDIF
NEXT i
OUTPUT Pos, Level[Pos]

Pos stores an index, so it is always a number from 1 to 5. Level[Pos] is the value at that index.

iLevel[i]Level[Pos] beforeLevel[i] > Level[Pos]?Pos after
253true2
325false2
455false2
545false2

The output is 2, 5: the index is 2 and the value stored there is 5. If the code printed i after the loop instead of Pos, it would show the loop variable, not the position of the largest value.

The mistake to watch for

A common slip is to add the index when the task asks for the values.

Mistaken code: Total ← Total + i

This adds 1 + 2 + 3 + 4 + 5 = 15, which is the sum of the positions.

The correction is Total ← Total + Level[i], which adds 3 + 5 + 2 + 5 + 4 = 19. When you trace, write the column headings i and Level[i] separately. The two columns cannot be mixed up if both are on the page.

Check yourself

Use the array Level above.

1. What are Level[4] and Level[Level[3]]?

Show answer

Level[4] is 5. Level[3] is 2, so Level[Level[3]] is Level[2], which is 5.

2. What does this code output?

FOR i ← 1 TO 5
   IF Level[i] = 5
      THEN
         OUTPUT i
   ENDIF
NEXT i
Show answer

It outputs the indexes where the value is 5. Level[2] and Level[4] equal 5, so the output is 2 then 4. It does not output the value 5.

3. What happens if the code tries to read Level[6]?

Show answer

The bounds are 1 to 5, so position 6 does not exist. The program would stop with an error. In a trace you write “index out of bounds”.

Where this leads next

With loops, faults and arrays covered, test everything together in the repetition and arrays practice set. To watch the index and the value change on each pass, use the pseudocode trace trainer.

A teacher in Computer Science tuition can ask you to explain, line by line, what each bracketed number means in your own attempts.

Questions people ask

What is the difference between an index and a value?

The index is the position of an item, such as 3 in Level[3]. The value is what is stored at that position, such as 2 if Level[3] holds 2. The index labels the box. The value is what is inside the box.

Does an array start at 0 or 1 in Cambridge pseudocode?

The bounds are written in the declaration, for example ARRAY[1:5], so read them there. Python lists start at 0. When a question gives bounds, use exactly those. Check the notation for your exam year on the Cambridge syllabus page.

What happens if I use an index outside the bounds?

The position does not exist, so the program would fail with an error. In a trace, write that the index is out of bounds rather than inventing a value. Many off-by-one errors end this way.

Updated:

Your next step

If index and value get mixed up only when the two look alike, a teacher can build arrays where they collide on purpose and show you a trace habit that keeps them apart.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80.

Tuition is arranged with a parent or guardian. Send them this page on WhatsApp and they can enquire for you.

Parents: enquire here

  • 9,000+ students helped through our service
  • 9+ years helping IGCSE students