← IGCSE Computer Science Paper 2

Linear search in pseudocode

Checks each array element in order from the first, comparing it with the target, until a match is found or the end of the array is reached.

Understanding Linear search in pseudocode in IGCSE Computer Science Paper 2

What it does: Setting the flag to TRUE records that the target has been found, so the loop can stop and the result can be reported. A linear search checks each element in order, beginning with the first one. Starting in the middle is a binary search, which needs sorted data. A comparison in a condition uses =. The ← arrow is only for assignment. In a WHILE loop you must add 1 to Index yourself, with Index ← Index + 1. Without it the loop repeats on the same element.

Common mistakes: A linear search starts at the first element and works forwards. FOR Pos ← 5 TO 1 counts down without a STEP, so the loop would not run properly. It should be FOR Pos ← 1 TO 5. Found starts as FALSE, so the condition Found AND ... is false straight away and the loop never runs. To keep searching until a match is found, the condition must use NOT Found.

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 242–253) and word search (Modules 232–253).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Where does a linear search start looking in an array?
At the first elementAt the middle element
Matches
IF Item[Index] ← Target THEN
↓
Uses the assignment arrow ← instead of = to compare. It should be IF Item[Index] = Target THEN.
Applying
Meanings
Term
linear search
Meaning
Checks each array element in order from the first, comparing it with the target, until a match is found or the end of the array is reached.
Practice
Found ← FALSE
Testing
Gaps
Found ←
Check Answer
Spots
1Found ← FALSE
2FOR Pos ← 5 TO 1

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

The 'Scorpion' badge — Venom Debugger, 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. Where does a linear search start looking in an array?
A. At the first element ✓
B. At the middle element
A linear search checks each element in order, beginning with the first one. Starting in the middle is a binary search, which needs sorted data.
2. Which line correctly compares an array element with the target in pseudocode?
A. IF Names[Index] ← Target THEN
B. IF Names[Index] = Target THEN ✓
A comparison in a condition uses =. The ← arrow is only for assignment.
3. What is the best way to set up the found flag before the search starts?
A. Found ← FALSE ✓
B. Found ← TRUE
We assume the target has not been found until a match is seen. The flag is only set to TRUE inside the IF when a match is made.
4. Which data type should the Found flag be declared as?
A. DECLARE Found : BOOLEAN ✓
B. DECLARE Found : INTEGER
A found flag has only two states, TRUE or FALSE, so it is a BOOLEAN.
5. Which WHILE condition lets a linear search stop early once the target is found?
A. WHILE Found = TRUE AND Index <= 10 DO
B. WHILE Found = FALSE AND Index <= 10 DO ✓
The loop must keep going while the target has NOT been found and there are elements left. If Found = TRUE were the condition, the loop would never start because Found begins as FALSE.

Match the Pairs

WHILE Index <= 10 AND Found = FALSE DO → Keeps searching only while elements remain and no match has been found, so the search stops early
IF Item[Index] = Target THEN → Compares the current array element with the value being searched for
A BOOLEAN found flag starts as FALSE and is set to TRUE when the target is matched → Found ← TRUE
If the loop ends and the flag is still FALSE, tell the user the target was not found → IF Found = FALSE THEN OUTPUT "Not found" ENDIF
IF Item[Index] ← Target THEN → Uses the assignment arrow ← instead of = to compare. It should be IF Item[Index] = Target THEN.
Index ← 1 WHILE Index <= 10 AND Found = FALSE DO IF Item[Index] = Target THEN Found ← TRUE ENDIF ENDWHILE → Index is never increased inside the loop, so the same element is checked forever. Add Index ← Index + 1 inside the loop.

Key Term

linear search
Checks each array element in order from the first, comparing it with the target, until a match is found or the end of the array is reached.

Fill in the Blanks

1. Found ← FALSE Index ← 1 WHILE Index <= 10 AND Found = FALSE DO
Hint: Type the BOOLEAN value the found flag starts with before anything has been checked.
2. Index ← 1 WHILE Index <= 10 AND Found = FALSE DO IF Item[Index] = Target THEN
Hint: Type the logical operator that makes the loop continue only while both conditions are true.
3. WHILE Index <= 10 AND Found = FALSE DO IF Item[Index] = Target THEN Found ← TRUE
Hint: Type the relational operator that compares the current element with the target.
4. Index ← Index + 1 ENDWHILE IF Found = FALSE THEN
Hint: Type the keyword that closes the WHILE loop.
5. IF Found = FALSE THEN OUTPUT "Not found" ENDIF
Hint: Type the keyword that displays a message when the target is not in the array.

Spot the Error

Question 1
A Found ← FALSE
B FOR Pos ← 5 TO 1
C IF Names[Pos] = Target THEN
D Found ← TRUE
E ENDIF
F NEXT Pos
Line B — A linear search starts at the first element and works forwards. FOR Pos ← 5 TO 1 counts down without a STEP, so the loop would not run properly. It should be FOR Pos ← 1 TO 5.
Question 2
A WHILE Found AND Pos < 6 DO
B IF Names[Pos] = Target THEN
C Found ← TRUE
D ENDIF
E Pos ← Pos + 1
F ENDWHILE
Line A — Found starts as FALSE, so the condition Found AND ... is false straight away and the loop never runs. To keep searching until a match is found, the condition must use NOT Found.
Question 3
A Found ← "FALSE"
B FOR Pos ← 1 TO 5
C IF Scores[Pos] = Target THEN
D Found ← TRUE
E ENDIF
F NEXT Pos
Line A — Found is a BOOLEAN flag. Putting quotes around FALSE makes it a string. It should be Found ← FALSE with no quotes.
Question 4
A FOR Index ← 1 TO 5
B IF Item = Target THEN
C OUTPUT "Found at ", Index
D ENDIF
E NEXT Index
Line B — The condition compares the whole array with the target. Each element must be compared in turn, so it should be IF Item[Index] = Target THEN.
Question 5
A Found ← FALSE
B FOR Pos ← 1 TO 8
C IF Words[Pos] <> Target THEN
D Found ← TRUE
E ENDIF
F NEXT Pos
Line C — A linear search looks for an element equal to the target. Using <> would flag every element that does not match. It should be IF Words[Pos] = Target THEN.

Student with a class code? Join your class →