← IGCSE Computer Science Paper 2

Searching an array for a value

A loop checks every element against the wanted value. On a match, set Found to TRUE and record the index. If Found is still FALSE at the end, OUTPUT a not found message.

Understanding Searching an array for a value in IGCSE Computer Science Paper 2

What it does: The loop counter Index holds the position of the current element, so copying it into Position records where the value was found. Found starts as FALSE because nothing has been found yet. It only becomes TRUE when a match is made. The loop must run from the lower bound 1 to the upper bound 10. Stopping at 9 would miss the last element. Use = to compare two values. The ← arrow is for assignment, so it cannot be used to test for a match. If every element was checked and none matched, Found never changed to TRUE. The program should then OUTPUT a message such as "Not found".

Common mistakes: When the element matches the target, Found must be set to TRUE. Setting it to FALSE means a match is never recorded. It should be Found ← TRUE. The IF condition compares two values, so it needs the comparison operator =. The ← arrow is only for assignment. It should be IF Names[Index] = Wanted THEN.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 459–468) and word search (Modules 459–478).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Before the search loop starts, how should the Found flag be set up?
Found ← FALSEFound ← TRUE
Matches
FOR Index ← 1 TO 10 IF Scores[Index] = Target THEN Found ← TRUE ELSE OUTPUT "Not found" ENDIF NEXT Index
↓
The not found message is output inside the loop for every element that does not match, instead of once after the loop has checked all elements.
Applying
Meanings
Term
linear search
Meaning
A loop checks every element against the wanted value. On a match, set Found to TRUE and record the index. If Found is still FALSE at the end, OUTPUT a not found message.
Practice
Found ← FALSE
Testing
Gaps
Found ←
Check Answer
Spots
3 IF Marks[Index] = Target THEN
4 Found ← FALSE

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Wolverine' badge — Fearless Coder, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Before the search loop starts, how should the Found flag be set up?
A. Found ← FALSE ✓
B. Found ← TRUE
Found starts as FALSE because nothing has been found yet. It only becomes TRUE when a match is made.
2. Names is declared as ARRAY[1:10] OF STRING. Which loop header checks every element?
A. FOR Index ← 1 TO 10 ✓
B. FOR Index ← 1 TO 9
The loop must run from the lower bound 1 to the upper bound 10. Stopping at 9 would miss the last element.
3. Which line correctly checks whether the current element is the wanted value?
A. IF Names[Index] = SearchName THEN ✓
B. IF Names[Index] ← SearchName THEN
Use = to compare two values. The ← arrow is for assignment, so it cannot be used to test for a match.
4. A match has been found. Which pair of statements should run?
A. Found ← FALSE and Position ← SearchName
B. Found ← TRUE and Position ← Index ✓
On a match, set Found to TRUE and store the index. Position records where the value was found, so it takes the value of Index, not the value searched for.
5. Which statement about a linear search is correct?
A. It only checks the middle element of the array
B. It compares the elements one at a time, using a loop ✓
A linear search uses a loop to check each element in turn against the wanted value.

Match the Pairs

Found ← FALSE → Set a flag before the loop starts to show that the wanted value has not been found yet
IF Names[Index] = Target THEN → Compare the current element with the wanted value to see if it is a match
When a match is found, record its position by storing the loop counter in a variable → Position ← Index
After the loop ends, if the flag is still FALSE, tell the user that the value is not in the array → IF Found = FALSE THEN OUTPUT "Not found"
FOR Index ← 1 TO 10 IF Scores[Index] = Target THEN Found ← TRUE ELSE OUTPUT "Not found" ENDIF NEXT Index → The not found message is output inside the loop for every element that does not match, instead of once after the loop has checked all elements.
Found ← TRUE FOR Index ← 1 TO 10 IF Scores[Index] = Target THEN Position ← Index ENDIF NEXT Index → The Found flag is set to TRUE before the loop and is never set inside it, so the program thinks the value was found even when it was not.

Key Term

linear search
A loop checks every element against the wanted value. On a match, set Found to TRUE and record the index. If Found is still FALSE at the end, OUTPUT a not found message.

Fill in the Blanks

1. Found ← FALSE FOR Index ← 1 TO 10 IF Marks[Index] = Target THEN
Hint: Type the Boolean value the flag holds before the search begins.
2. DECLARE Names : ARRAY[1:10] OF STRING FOR Index ← 1 TO 10 IF Names[Index] = Target THEN
Hint: Type the upper bound so the loop visits every element of the array.
3. FOR Index ← 1 TO 10 IF Marks[Index] = Target THEN Found ← TRUE
Hint: Type the comparison operator that tests whether the element matches the wanted value.
4. IF Marks[Index] = Target THEN Found ← TRUE Position ← Index
Hint: Type the loop variable that holds the current index, so the position of the match is recorded.
5. IF Found = FALSE THEN OUTPUT "Target not found" ENDIF
Hint: Type the keyword that displays a message on the screen.

Spot the Error

Question 1
A Found ← FALSE
B FOR Index ← 1 TO 10
C IF Marks[Index] = Target THEN
D Found ← FALSE
E ENDIF
F NEXT Index
Line D — When the element matches the target, Found must be set to TRUE. Setting it to FALSE means a match is never recorded. It should be Found ← TRUE.
Question 2
A Found ← FALSE
B FOR Index ← 1 TO 5
C IF Names[Index] ← Wanted THEN
D Found ← TRUE
E ENDIF
F NEXT Index
Line C — The IF condition compares two values, so it needs the comparison operator =. The ← arrow is only for assignment. It should be IF Names[Index] = Wanted THEN.
Question 3
A Found ← TRUE
B FOR Index ← 1 TO 8
C IF Codes[Index] = Wanted THEN
D Found ← TRUE
E ENDIF
F NEXT Index
Line A — The flag must start as FALSE before the search begins, because nothing has been found yet. Starting as TRUE would make the search report a match even when there is none. It should be Found ← FALSE.
Question 4
A Found ← FALSE
B FOR Index ← 1 TO 8
C IF Codes[Index] = Wanted
D Found ← TRUE
E ENDIF
F NEXT Index
Line C — An IF statement must have THEN after its condition. It should be IF Codes[Index] = Wanted THEN.
Question 5
A Found = FALSE
B FOR Index ← 1 TO 4
C IF Ids[Index] = Wanted THEN
D Found ← TRUE
E ENDIF
F NEXT Index
Line A — Giving the flag its starting value is an assignment, so it needs the ← arrow. Using = only compares values and does not store FALSE in Found. It should be Found ← FALSE.

Student with a class code? Join your class →