← IGCSE Computer Science Paper 2

Linear search with a FOR loop and a Found flag

Sets a BOOLEAN flag to FALSE before a FOR loop. The loop sets Found to TRUE if an element equals the target. After the loop, Found shows whether the target was seen.

Understanding Linear search with a FOR loop and a Found flag in IGCSE Computer Science Paper 2

What it does: A flag that holds only TRUE or FALSE needs the BOOLEAN data type, declared before it is used. The result is output after the loop has checked every element, using the flag to choose the message. If Found ← FALSE were inside the loop, it would reset on every pass and wipe out an earlier TRUE. You must use the index to pick one element. Names on its own is the whole array, not a single item. A match means the target has been seen, so the flag is set to TRUE. Found was set to FALSE at the start and never changed, because no element matched.

Common mistakes: When the target is matched, the flag must be set to TRUE. Setting it to FALSE means the search can never record a success. It should be Found ← TRUE. The flag must start as FALSE before the loop, because nothing has been found yet. Starting as TRUE would report a match even when the target is missing. It should be Found ← FALSE.

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 line correctly sets up the flag before a linear search loop?
Found ← TRUEFound ← FALSE
Matches
FOR Index ← 1 TO 5 Found ← FALSE IF Names[Index] = Target THEN Found ← TRUE NEXT Index
↓
Found ← FALSE is inside the loop, so it resets the flag on every pass and a match can be lost. It must be set once before the loop.
Applying
Meanings
Term
Found ← FALSE
Meaning
Sets a BOOLEAN flag to FALSE before a FOR loop. The loop sets Found to TRUE if an element equals the target. After the loop, Found shows whether the target was seen.
Practice
DECLARE Found : BOOLEAN
Testing
Gaps
DECLARE Found :
Check Answer
Spots
3 IF Names[Pos] = Target THEN
4 Found ← FALSE

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

The 'Parrotfish' badge — Function Builder, 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 line correctly sets up the flag before a linear search loop?
A. Found ← FALSE ✓
B. Found ← TRUE
The flag starts as FALSE because nothing has been found yet. The loop changes it to TRUE only if the target is seen.
2. Which declaration is correct for the Found flag?
A. DECLARE Found : STRING
B. DECLARE Found : BOOLEAN ✓
Found only holds TRUE or FALSE, so its data type is BOOLEAN.
3. Where should Found ← FALSE be placed?
A. Inside the loop, as its first line
B. Before the FOR loop starts ✓
If Found ← FALSE were inside the loop, it would reset on every pass and wipe out an earlier TRUE.
4. Which IF statement inside the loop checks the current element against the target?
A. IF Names[Index] = Target THEN ✓
B. IF Names = Target THEN
You must use the index to pick one element. Names on its own is the whole array, not a single item.
5. What happens when Names[Index] = Target is true?
A. Found ← TRUE ✓
B. Found ← FALSE
A match means the target has been seen, so the flag is set to TRUE.

Match the Pairs

Found ← FALSE → Sets the flag to FALSE before the loop starts, meaning the target has not been seen yet
IF Names[Index] = Target THEN Found ← TRUE → Compares the current array element with the target and sets the flag to TRUE if they are equal
Declare a BOOLEAN variable to hold whether the target was found → DECLARE Found : BOOLEAN
After the loop ends, output a message only if the flag shows the target was seen → IF Found = TRUE THEN OUTPUT "Found" ELSE OUTPUT "Not found" ENDIF
FOR Index ← 1 TO 5 Found ← FALSE IF Names[Index] = Target THEN Found ← TRUE NEXT Index → Found ← FALSE is inside the loop, so it resets the flag on every pass and a match can be lost. It must be set once before the loop.
FOR Index ← 1 TO 5 IF Names[Index] = Target THEN Found ← TRUE NEXT Index OUTPUT Found ← TRUE → The output line assigns TRUE to Found instead of reporting the result, so it always shows a match. It should output Found or a message based on its value.

Key Term

Found ← FALSE
Sets a BOOLEAN flag to FALSE before a FOR loop. The loop sets Found to TRUE if an element equals the target. After the loop, Found shows whether the target was seen.

Fill in the Blanks

1. DECLARE Found : BOOLEAN DECLARE Index : INTEGER
Hint: Type the data type that can only hold TRUE or FALSE.
2. Found ← FALSE FOR Index ← 1 TO 5 IF Names[Index] = Target THEN
Hint: Type the starting value of the flag before the loop begins, meaning nothing has been found yet.
3. FOR Index ← 1 TO 5 IF Names[Index] = Target THEN Found ← TRUE
Hint: Type the comparison operator that checks whether the element equals the target.
4. IF Names[Index] = Target THEN Found ← TRUE ENDIF
Hint: Type the value that records the target was seen.
5. NEXT Index IF Found = TRUE THEN OUTPUT "Name found"
Hint: Type the name of the flag variable that is tested after the loop has finished.

Spot the Error

Question 1
A Found ← FALSE
B FOR Pos ← 1 TO 5
C IF Names[Pos] = Target THEN
D Found ← FALSE
E ENDIF
F NEXT Pos
Line D — When the target is matched, the flag must be set to TRUE. Setting it to FALSE means the search can never record a success. It should be Found ← TRUE.
Question 2
A Found ← TRUE
B FOR Pos ← 1 TO 8
C IF Teams[Pos] = Target THEN
D Found ← TRUE
E ENDIF
F NEXT Pos
Line A — The flag must start as FALSE before the loop, because nothing has been found yet. Starting as TRUE would report a match even when the target is missing. It should be Found ← FALSE.
Question 3
A Found ← FALSE
B FOR Pos ← 1 TO 6
C IF Names[Pos] <> Target THEN
D Found ← TRUE
E ENDIF
F NEXT Pos
Line C — The search must test whether the element EQUALS the target. <> means not equal, so Found would be set for the wrong elements. It should be IF Names[Pos] = Target THEN.
Question 4
A Found ← FALSE
B FOR Pos ← 1 TO 4
C IF Words[Pos] = Target THEN
D Found = TRUE
E ENDIF
F NEXT Pos
Line D — A single = compares values. It does not store anything. To set the flag you use the assignment arrow: Found ← TRUE.
Question 5
A IF Found = FALSE THEN
B OUTPUT "Found"
C ELSE
D OUTPUT "Not found"
E ENDIF
Line A — After the loop, the TRUE branch should report success. Testing for FALSE swaps the two messages. It should be IF Found = TRUE THEN.

Student with a class code? Join your class →