← IGCSE Computer Science Paper 2

Outputting the result of a linear search

After the search loop, check the Found flag: if TRUE, output the position (Index) where the target was found, otherwise output a not-found message. Covers first, last and missing items.

Understanding Outputting the result of a linear search in IGCSE Computer Science Paper 2

What it does: A target at the first item sets Found to TRUE at Index 1, so the output is Found at 1. The result is only known once the search loop is over. Then one IF...ELSE on the Found flag outputs either the position or the not-found message.

Common mistakes: ELSE never has THEN after it in Cambridge pseudocode. Only IF takes THEN. The line should be just ELSE. ← assigns a value. A condition must compare, so it should use =: IF Found = TRUE 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 296–306) and word search (Modules 296–316).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Where does the IF Found check go in a linear search?
Inside the loop, before the first comparisonAfter the loop has finished
Matches
FOR Index ← 1 TO 10 IF List[Index] = Target THEN Found ← TRUE ENDIF IF Found = TRUE THEN OUTPUT "Found at ", Index ELSE OUTPUT "Not found" ENDIF NEXT Index
↓
The IF…ELSE on Found is inside the loop, so it outputs a result on every pass. It should come after NEXT Index, once the search is finished.
Applying
Meanings
Term
IF Found = TRUE THEN OUTPUT "Found at ", Index ELSE OUTPUT "Not found" ENDIF
Meaning
After the search loop, check the Found flag: if TRUE, output the position (Index) where the target was found, otherwise output a not-found message. Covers first, last and missing items.
Practice
Found ← FALSE
Testing
Gaps
IF = TRUE THEN
Check Answer
Spots
2 OUTPUT "Found at ", Index
3ELSE THEN

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

The 'Ermine' badge — Tundra Streak, 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 the IF Found check go in a linear search?
A. After the loop has finished ✓
B. Inside the loop, before the first comparison
The result is only known once the search loop is over. Then one IF...ELSE on the Found flag outputs either the position or the not-found message.
2. Which statement outputs the position when the target has been found?
A. IF Found = FALSE THEN OUTPUT "Found at ", Index
B. IF Found = TRUE THEN OUTPUT "Found at ", Index ✓
Found is set to TRUE when the target matches, so the position is only output when Found = TRUE.
3. Data = [4, 8, 15, 16, 23] with positions 1 to 5. The target is 4, the FIRST item. What is output?
A. Found at 1 ✓
B. Not found
The target matches at Index 1, so Found becomes TRUE and the IF part outputs Found at 1.
4. Data = [4, 8, 15, 16, 23] with positions 1 to 5. The target is 23, the LAST item. What is output?
A. Found at 4
B. Found at 5 ✓
The match is at the last position, Index 5. A target at the last item is still found, so the output is Found at 5.
5. Data = [4, 8, 15, 16, 23]. The target is 99, which is MISSING. What is output?
A. Not found ✓
B. Found at 5
No item matches, so Found stays FALSE and the ELSE part runs. Index may be 5 after the loop, but the flag shows the target was never found.

Match the Pairs

IF Found = TRUE THEN OUTPUT "Found at ", Index ENDIF → Outputs the position of the target, but only when the search loop has found it
ELSE OUTPUT "Not found" → Runs when the Found flag is still FALSE, so the target is missing from the array
If the target is the first item, the search loop finds it on its first pass, so Found is TRUE and Index is 1 for the output → Found ← TRUE Index ← 1 OUTPUT "Found at ", Index
If the target is missing, the loop ends without changing Found, so the ELSE branch outputs a not-found message → IF Found = FALSE THEN OUTPUT "Not found" ENDIF
FOR Index ← 1 TO 10 IF List[Index] = Target THEN Found ← TRUE ENDIF IF Found = TRUE THEN OUTPUT "Found at ", Index ELSE OUTPUT "Not found" ENDIF NEXT Index → The IF…ELSE on Found is inside the loop, so it outputs a result on every pass. It should come after NEXT Index, once the search is finished.
IF Found = TRUE THEN OUTPUT "Not found" ELSE OUTPUT "Found at ", Index ENDIF → The two outputs are the wrong way round. TRUE should output Found at with the Index, and ELSE should output Not found.

Key Term

IF Found = TRUE THEN OUTPUT "Found at ", Index ELSE OUTPUT "Not found" ENDIF
After the search loop, check the Found flag: if TRUE, output the position (Index) where the target was found, otherwise output a not-found message. Covers first, last and missing items.

Fill in the Blanks

1. IF Found = TRUE THEN OUTPUT "Found at ", Index ELSE
Hint: Type the flag variable that records whether the target was found during the loop.
2. IF Found = TRUE THEN OUTPUT "Found at ", Index ELSE
Hint: Type the variable that holds the position of the target in the array.
3. IF Found = TRUE THEN OUTPUT "Found at ", Index ELSE
Hint: Type the keyword that starts the branch used when the condition is not true.
4. ELSE OUTPUT "Not found" ENDIF
Hint: Type the command that displays a message to the user when the target is missing.
5. ELSE OUTPUT "Not found" ENDIF
Hint: Type the keyword that closes the selection after the search result has been checked.

Spot the Error

Question 1
A IF Found = TRUE THEN
B OUTPUT "Found at ", Index
C ELSE THEN
D OUTPUT "Not found"
E ENDIF
Line C — ELSE never has THEN after it in Cambridge pseudocode. Only IF takes THEN. The line should be just ELSE.
Question 2
A IF Found ← TRUE THEN
B OUTPUT "Found at ", Index
C ELSE
D OUTPUT "Not found"
E ENDIF
Line A — ← assigns a value. A condition must compare, so it should use =: IF Found = TRUE THEN.
Question 3
A NEXT Index
B IF Found = TRUE
C OUTPUT "Found at ", Index
D ELSE
E OUTPUT "Not found"
F ENDIF
Line B — The IF line is missing THEN. It should be IF Found = TRUE THEN.
Question 4
A IF Found = TRUE THEN
B OUTPUT "Found at " Index
C ELSE
D OUTPUT "Not found"
E ENDIF
Line B — A comma is needed between the message and the variable. It should be OUTPUT "Found at ", Index.
Question 5
A IF Found = TRUE THEN
B OUTPUT "Found at ", Index
C ELSE
D OUTPUT "Found at ", Index
E ENDIF
Line D — The ELSE branch runs when the target was never found, so it must output "Not found", not a position.

Student with a class code? Join your class →