← IGCSE Computer Science Paper 2

Linear search that stops when the target is found

Keeps searching while the target has not been found AND the end of the list has not been reached, so the loop stops early as soon as Found becomes TRUE.

Understanding Linear search that stops when the target is found in IGCSE Computer Science Paper 2

What it does: Position ← Index stores the index at the moment of the match, before the loop moves on and changes Index. A WHILE loop has no automatic counter, so Index must be increased inside the loop or the same item is checked forever. Found = FALSE is no longer true, so the whole AND condition is false and the search ends straight away. REPEAT...UNTIL tests its condition at the end, after the body has run once. WHILE tests at the start, so its body might never run. The loop stops as soon as Found becomes TRUE, so only items 1, 2 and 3 are checked. A FOR loop with no early exit would check all 10.

Common mistakes: The test must use = so Found only becomes TRUE when the item equals the target. With <> the loop would stop on the first item that does NOT match. It should be IF List[I] = Target THEN. ← assigns a value, it does not compare. A condition in an IF must use = to compare. It should be IF Names[I] = Target.

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 286–295) and word search (Modules 275–295).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which WHILE condition makes a linear search of a 10-item array stop as soon as the target is found?
WHILE Found = FALSE OR Index <= 10 DOWHILE Found = FALSE AND Index <= 10 DO
Matches
WHILE Found = FALSE OR Index <= 10 DO
↓
OR keeps the loop running while either condition is true, so after the target is found the loop carries on to the end of the list. It should use AND so the loop stops as soon as Found is TRUE.
Applying
Meanings
Term
WHILE Found = FALSE AND Index <= 10 DO
Meaning
Keeps searching while the target has not been found AND the end of the list has not been reached, so the loop stops early as soon as Found becomes TRUE.
Practice
Index ← 1
Testing
Gaps
WHILE Found = FALSE Index <= 10 DO
Check Answer
Spots
1WHILE NOT Found AND I <= 10 DO
2 IF List[I] <> Target THEN

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

The 'Desert Tortoise' badge — Desert Master, 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. Which WHILE condition makes a linear search of a 10-item array stop as soon as the target is found?
A. WHILE Found = FALSE AND Index <= 10 DO ✓
B. WHILE Found = FALSE OR Index <= 10 DO
The loop must carry on only while BOTH are true: not found yet AND not past the end. With OR, the loop would keep going after the end of the list was reached.
2. What happens to the loop WHILE Found = FALSE AND Index <= 10 when Found is set to TRUE?
A. The condition becomes false and the loop stops early ✓
B. The loop carries on until Index reaches 10
Found = FALSE is no longer true, so the whole AND condition is false and the search ends straight away.
3. Where should Index ← Index + 1 go in a WHILE linear search?
A. Before the loop, so it only runs once
B. Inside the loop, so the next item is checked each time ✓
A WHILE loop does not count for you. Without Index ← Index + 1 inside the loop, the same item is checked forever and the loop never ends.
4. Before a WHILE linear search starts, which setup is correct?
A. Found ← FALSE and Index ← 1 ✓
B. Found ← TRUE and Index ← 1
Found must start as FALSE because the target has not been found yet. If it started as TRUE, the loop would never run.
5. A student wants to remember where the target was found. Which line inside the IF is correct?
A. Position ← Found
B. Position ← Index ✓
Index holds the position of the item being checked. Found is only TRUE or FALSE, so it cannot store a position.

Match the Pairs

WHILE Found = FALSE AND Index <= 10 DO → Keep looping only while the target has not been found and the end of the list has not been reached
UNTIL Found = TRUE OR Index > 10 → The REPEAT loop stops when the target is found or the end of the list is passed
Save the position where the target was found so it can be output after the loop ends → IF Numbers[Index] = Target THEN Found ← TRUE Position ← Index ENDIF
Move on to the next item in the list each time round a WHILE loop → Index ← Index + 1
WHILE Found = FALSE OR Index <= 10 DO → OR keeps the loop running while either condition is true, so after the target is found the loop carries on to the end of the list. It should use AND so the loop stops as soon as Found is TRUE.
WHILE Found = FALSE AND Index <= 10 DO IF Numbers[Index] = Target THEN Found ← TRUE ENDIF ENDWHILE → Index ← Index + 1 is missing inside the loop, so Index never changes and the loop repeats the same item forever when the target is not at position 1.

Key Term

WHILE Found = FALSE AND Index <= 10 DO
Keeps searching while the target has not been found AND the end of the list has not been reached, so the loop stops early as soon as Found becomes TRUE.

Fill in the Blanks

1. WHILE Found = FALSE AND Index <= 10 DO
Hint: Type the logical operator that means both conditions must be true to keep looping.
2. WHILE Found = FALSE AND Index <= 10 DO Index ← Index + 1 ENDWHILE
Hint: Type the arithmetic operator that moves the search on to the next position.
3. IF List[Index] = Target THEN Found ← TRUE Position ← Index
Hint: Type the Boolean value that makes the loop condition fail so the search stops.
4. UNTIL Found = TRUE OR Index > 10
Hint: Type the logical operator that ends the REPEAT loop when either stopping condition is met.
5. Found ← FALSE Index ← 1 WHILE Found = FALSE AND Index <= 10 DO
Hint: Type the comparison operator that keeps the index within the end of the list.

Spot the Error

Question 1
A WHILE NOT Found AND I <= 10 DO
B IF List[I] <> Target THEN
C Found ← TRUE
D ENDIF
E I ← I + 1
F ENDWHILE
Line B — The test must use = so Found only becomes TRUE when the item equals the target. With <> the loop would stop on the first item that does NOT match. It should be IF List[I] = Target THEN.
Question 2
A WHILE NOT Found AND I <= 10 DO
B IF Names[I] ← Target
C THEN Found ← TRUE
D ELSE I ← I + 1
E ENDIF
F ENDWHILE
Line B — ← assigns a value, it does not compare. A condition in an IF must use = to compare. It should be IF Names[I] = Target.
Question 3
A WHILE NOT Found AND I <= 10 DO
B IF Marks[I] = Target
C THEN Found ← TRUE
D ELSE I ← I - 1
E ENDIF
F ENDWHILE
Line D — The index must move forward to the next item each time the target is not found. Subtracting 1 would go backwards and loop forever. It should be ELSE I ← I + 1.
Question 4
A WHILE NOT Found OR I <= 10 DO
B IF List[I] = Target THEN
C Found ← TRUE
D ENDIF
E I ← I + 1
F ENDWHILE
Line A — The loop must stop when the target is found OR the end of the list is reached, so it must keep going only while BOTH conditions are true. OR would keep looping after Found is TRUE. It should be AND.
Question 5
A REPEAT
B IF List[I] = Target THEN
C Found ← TRUE
D ENDWHILE
E I ← I + 1
F UNTIL Found = TRUE OR I > 10
Line D — An IF statement is closed with ENDIF. ENDWHILE closes a WHILE loop, and this loop is a REPEAT...UNTIL. Line 4 should be ENDIF.

Student with a class code? Join your class →