Coding Sheets, Practice & Learning Support: Algorithm Tracing Practice Worksheets (Copy)
Algorithm Tracing Practice Worksheets
How To Use These Worksheets Properly
- For each question:
- Trace line by line
- Update variables in order
- Do not skip iterations
- Marks are awarded for:
- Correct intermediate values
- Correct final output
- Always use a trace table, even if not asked
Worksheet 1: Basic FOR Loop Tracing
Algorithm
total ← 0
FOR i ← 1 TO 5
total ← total + i
NEXT i
OUTPUT total
Student Task
- Complete the trace table
- State the final output
Trace Table
| i | total before | total after |
|---|---|---|
| 1 | 0 | |
| 2 | ||
| 3 | ||
| 4 | ||
| 5 |
Worksheet 2: FOR Loop With Conditional
Algorithm
count ← 0
FOR i ← 1 TO 8
IF i MOD 2 = 0 THEN
count ← count + 1
ENDIF
NEXT i
OUTPUT count
Student Task
- Trace only when condition is TRUE
- Identify which values increment
count
Trace Table
| i | i MOD 2 = 0 | count |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 | ||
| 4 | ||
| 5 | ||
| 6 | ||
| 7 | ||
| 8 |
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Worksheet 3: WHILE Loop Tracing
Algorithm
x ← 10
count ← 0
WHILE x > 0 DO
x ← x - 3
count ← count + 1
ENDWHILE
OUTPUT x
OUTPUT count
Student Task
- Trace until loop condition becomes FALSE
- Record final values of
xandcount
Trace Table
| Iteration | x before | x after | count |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 |
Worksheet 4: REPEAT UNTIL Loop Tracing
Algorithm
total ← 0
num ← 0
REPEAT
num ← num + 2
total ← total + num
UNTIL num >= 10
OUTPUT total
Student Task
- Remember: REPEAT runs at least once
- Stop when condition becomes TRUE
Trace Table
| Step | num | total | num >= 10 |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 |
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Worksheet 5: Flag-Controlled Loop Tracing
Algorithm
found ← FALSE
i ← 1
arr ← [4, 7, 9, 2, 6]
target ← 2
WHILE i <= 5 AND found = FALSE DO
IF arr[i] = target THEN
found ← TRUE
ELSE
i ← i + 1
ENDIF
ENDWHILE
OUTPUT i
OUTPUT found
Student Task
- Identify where the loop stops
- Trace both
iandfound
Trace Table
| i | arr[i] | found before | found after |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 |
Worksheet 6: Sentinel-Controlled Loop Tracing
Algorithm
total ← 0
REPEAT
INPUT num
IF num <> -1 THEN
total ← total + num
ENDIF
UNTIL num = -1
OUTPUT total
Input Sequence
3, 5, 2, -1
Student Task
- Show how sentinel is excluded
- Calculate final
total
Trace Table
| Input num | num = -1 | total |
|---|---|---|
| 3 | ||
| 5 | ||
| 2 | ||
| -1 |
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Worksheet 7: Array Traversal Tracing
Algorithm
arr ← [2, 4, 6, 8, 10]
sum ← 0
FOR i ← 1 TO 5
sum ← sum + arr[i]
NEXT i
OUTPUT sum
Student Task
- Trace each array access
- Track cumulative
sum
Trace Table
| i | arr[i] | sum |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 | ||
| 4 | ||
| 5 |
Worksheet 8: Maximum-Finding Trace
Algorithm
arr ← [5, 3, 9, 1, 6]
max ← arr[1]
FOR i ← 2 TO 5
IF arr[i] > max THEN
max ← arr[i]
ENDIF
NEXT i
OUTPUT max
Student Task
- Track when
maxchanges
Trace Table
| i | arr[i] | max before | max after |
|---|---|---|---|
| 2 | |||
| 3 | |||
| 4 | |||
| 5 |
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Worksheet 9: Nested Loop Tracing (2D Table)
Algorithm
total ← 0
table ← [[1,2,3],[4,5,6]]
FOR row ← 1 TO 2
FOR col ← 1 TO 3
total ← total + table[row][col]
NEXT col
NEXT row
OUTPUT total
Student Task
- Trace row-by-row
- Track cumulative
total
Trace Table
| row | col | table[row][col] | total |
|---|---|---|---|
| 1 | 1 | ||
| 1 | 2 | ||
| 1 | 3 | ||
| 2 | 1 | ||
| 2 | 2 | ||
| 2 | 3 |
Worksheet 10: Bubble Sort Single-Pass Trace
Algorithm (One Pass Only)
arr ← [6, 2, 4, 1]
FOR j ← 1 TO 3
IF arr[j] > arr[j + 1] THEN
temp ← arr[j]
arr[j] ← arr[j + 1]
arr[j + 1] ← temp
ENDIF
NEXT j
OUTPUT arr
Student Task
- Trace only one pass
- Show array after each comparison
Trace Table
| j | Compared | Swap? | Array After |
|---|---|---|---|
| 1 | arr[1], arr[2] | ||
| 2 | arr[2], arr[3] | ||
| 3 | arr[3], arr[4] |
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Universal Tracing Checklist (Exam Use)
- Initialise variables first
- Evaluate loop condition before each iteration
- Update variables in correct order
- Stop immediately when condition fails
- Final output comes after loop ends
Generic Trace Table Templates (Copy For Any Question)
Loop Tracing
| Iteration | Variable(s) Before | Condition | Variable(s) After | Output |
|---|
Array Tracing
| Index | Value Accessed | Key Variables | Output |
|---|
Flag Tracing
| Step | Index | Condition Met | Flag | Loop Continues |
|---|
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
