Linear search with a FOR loop and a Found flag

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Linear search with a FOR loop and a Found flag

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly sets up the flag before a linear search loop?
A Found ← FALSE
B Found ← TRUE
2. Which declaration is correct for the Found flag?
A DECLARE Found : STRING
B DECLARE Found : BOOLEAN
3. Where should Found ← FALSE be placed?
A Inside the loop, as its first line
B Before the FOR loop starts
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
5. What happens when Names[Index] = Target is true?
A Found ← TRUE
B Found ← FALSE

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1Found ← FALSE ADECLARE Found : BOOLEAN
2IF Names[Index] = Target THEN Found ← TRUE BCompares the current array element with the target and sets the flag to TRUE if they are equal
3Declare a BOOLEAN variable to hold whether the target was found CThe 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.
4After the loop ends, output a message only if the flag shows the target was seen DIF Found = TRUE THEN OUTPUT "Found" ELSE OUTPUT "Not found" ENDIF
5FOR Index ← 1 TO 5 Found ← FALSE IF Names[Index] = Target THEN Found ← TRUE NEXT Index ESets the flag to FALSE before the loop starts, meaning the target has not been seen yet
6FOR Index ← 1 TO 5 IF Names[Index] = Target THEN Found ← TRUE NEXT Index OUTPUT Found ← TRUE FFound ← 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.

Definitions

Found ← FALSE

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
DECLARE Found : _______ DECLARE Index : INTEGER
2.
Found ← _____ FOR Index ← 1 TO 5 IF Names[Index] = Target THEN
3.
FOR Index ← 1 TO 5 IF Names[Index] _ Target THEN Found ← TRUE
4.
IF Names[Index] = Target THEN Found ← ____ ENDIF
5.
NEXT Index IF _____ = TRUE THEN OUTPUT "Name found"

Spot the Error

Shade in the letter of the line that contains an 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
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
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
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
Question 5
A IF Found = FALSE THEN
B OUTPUT "Found"
C ELSE
D OUTPUT "Not found"
E ENDIF

Answer Key

Linear search with a FOR loop and a Found flag
Section A: Quick Questions
Q1
A — Found ← FALSE
Q2
B — DECLARE Found : BOOLEAN
Q3
B — Before the FOR loop starts
Q4
A — IF Names[Index] = Target THEN
Q5
A — Found ← TRUE
Section B: Match the Pairs
Q1
Found ← FALSE → Sets the flag to FALSE before the loop starts, meaning the target has not been seen yet
Q2
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
Q3
Declare a BOOLEAN variable to hold whether the target was found → DECLARE Found : BOOLEAN
Q4
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
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
DECLARE Found : BOOLEAN DECLARE Index : INTEGER
Q2
Found ← FALSE FOR Index ← 1 TO 5 IF Names[Index] = Target THEN
Q3
FOR Index ← 1 TO 5 IF Names[Index] = Target THEN Found ← TRUE
Q4
IF Names[Index] = Target THEN Found ← TRUE ENDIF
Q5
NEXT Index IF Found = TRUE THEN OUTPUT "Name found"
Section E: Spot the Error
Q1
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.
Q2
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.
Q3
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.
Q4
D — A single = compares values. It does not store anything. To set the flag you use the assignment arrow: Found ← TRUE.
Q5
A — After the loop, the TRUE branch should report success. Testing for FALSE swaps the two messages. It should be IF Found = TRUE THEN.