Selection And Iteration Code Guides: Loop Tracing And Dry-Run Techniques (Copy)
Loop Tracing And Dry-Run Techniques
What Loop Tracing (Dry-Run) Means
- Loop tracing = manually simulating loop execution step-by-step
- Purpose
- Predict final output
- Detect logic errors
- Avoid examiner traps in trace / predict-output questions
- Examiners expect:
- Clear understanding of execution order
- Correct handling of loop conditions
- Accurate updates of control variables
Golden Rule Of Dry-Running
- Never jump steps
- Always follow this order:
- Check condition (if applicable)
- Execute body
- Update control variables
- Re-evaluate condition
Universal Tracing Table Method (Exam-Safe)
Step 1: Identify Control Variables
- Loop counters (
i,index,count) - Condition variables (
choice,num,found) - Accumulators (
total,sum) - Flags (
found,valid)
Step 2: Create A Trace Table
- Columns typically include:
- Iteration number
- Loop variable(s)
- Key variables
- Condition result
- Output (if any)
Tracing FOR Loops
Execution Order (FOR Loop)
- Counter initialised
- Condition implicitly checked
- Body executes
- Counter automatically updated
- Stops after end value exceeded
FOR Loop Trace Example 1 (Summation)
total ← 0
FOR i ← 1 TO 4
total ← total + i
NEXT i
OUTPUT total
| Iteration | i | total before | total after |
|---|---|---|---|
| 1 | 1 | 0 | 1 |
| 2 | 2 | 1 | 3 |
| 3 | 3 | 3 | 6 |
| 4 | 4 | 6 | 10 |
- Final output:
10
FOR Loop Trace Example 2 (STEP Value)
FOR i ← 2 TO 10 STEP 2
OUTPUT i
NEXT i
| Iteration | i | Output |
|---|---|---|
| 1 | 2 | 2 |
| 2 | 4 | 4 |
| 3 | 6 | 6 |
| 4 | 8 | 8 |
| 5 | 10 | 10 |
- Common trap
- Forgetting STEP value when tracing
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
Tracing WHILE Loops (Entry-Controlled)
Execution Order (WHILE Loop)
- Condition checked before body
- If FALSE → loop skipped entirely
- If TRUE → body runs
- Control variable updated
- Condition checked again
WHILE Loop Trace Example 1
i ← 1
WHILE i < 4 DO
OUTPUT i
i ← i + 1
ENDWHILE
| Step | i before | Condition (i < 4) | Output | i after |
|---|---|---|---|---|
| Start | 1 | TRUE | – | – |
| 1 | 1 | TRUE | 1 | 2 |
| 2 | 2 | TRUE | 2 | 3 |
| 3 | 3 | TRUE | 3 | 4 |
| End | 4 | FALSE | – | – |
- Output:
1 2 3
WHILE Loop Trace Example 2 (Sentinel)
total ← 0
INPUT num
WHILE num <> -1 DO
total ← total + num
INPUT num
ENDWHILE
OUTPUT total
Assume inputs: 4, 6, -1
| Step | num | total |
|---|---|---|
| Start | 4 | 0 |
| Loop 1 | 4 | 4 |
| Loop 2 | 6 | 10 |
| Exit | -1 | 10 |
- Sentinel value is not added
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
Tracing REPEAT UNTIL Loops (Exit-Controlled)
Execution Order (REPEAT UNTIL)
- Body executes first
- Condition checked after
- Loop repeats while condition is FALSE
- Stops when condition becomes TRUE
REPEAT UNTIL Trace Example 1
i ← 1
REPEAT
OUTPUT i
i ← i + 1
UNTIL i = 4
| Iteration | i before | Output | i after | Condition (i = 4) |
|---|---|---|---|---|
| 1 | 1 | 1 | 2 | FALSE |
| 2 | 2 | 2 | 3 | FALSE |
| 3 | 3 | 3 | 4 | TRUE |
- Output:
1 2 3
REPEAT UNTIL Trace Example 2 (Validation)
REPEAT
INPUT num
UNTIL num >= 1 AND num <= 5
Assume inputs: -3, 7, 4
| Attempt | num | Condition |
|---|---|---|
| 1 | -3 | FALSE |
| 2 | 7 | FALSE |
| 3 | 4 | TRUE |
- Loop always runs at least once
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
Tracing Nested Loops
Nested Loop Rule
- Inner loop completes fully for each outer loop iteration
- Total executions = outer × inner
Nested Loop Trace Example
FOR i ← 1 TO 2
FOR j ← 1 TO 3
OUTPUT i * j
NEXT j
NEXT i
| i | j | Output |
|---|---|---|
| 1 | 1 | 1 |
| 1 | 2 | 2 |
| 1 | 3 | 3 |
| 2 | 1 | 2 |
| 2 | 2 | 4 |
| 2 | 3 | 6 |
- Output order matters (row-major)
Tracing With Flags (Boolean Control)
Flag-Controlled Loop Example
found ← FALSE
index ← 1
WHILE index <= n AND found = FALSE DO
IF arr[index] = target THEN
found ← TRUE
ELSE
index ← index + 1
ENDIF
ENDWHILE
Trace focus:
- When does
foundchange? - Does loop stop immediately after TRUE?
- Does index exceed bounds?
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
High-Frequency Examiner Traps In Dry-Runs
Trap 1: Forgetting Initial Condition Check (WHILE)
- Loop may not run at all
- Always check starting condition first
Trap 2: Misreading REPEAT UNTIL Condition
- UNTIL condition = stop condition
- Students often trace it like WHILE (wrong)
Trap 3: Off-By-One Errors
- Loop bounds incorrect
- Missing first or last iteration
Trap 4: Updating Variable In Wrong Place
- Update before output vs after output
- Order changes results
Trap 5: Ignoring STEP Direction
- Negative STEP not applied mentally
- Loop assumed to increment
Examiner-Approved Dry-Run Checklist
- Identify loop type
- Write start values
- Create trace table
- Follow execution order strictly
- Update variables only where code says
- Stop exactly when condition fails / succeeds
Rapid Practice: Spot The Final Output
Practice 1
i ← 1
WHILE i <= 3 DO
OUTPUT i * 2
i ← i + 1
ENDWHILE
- Output:
2 4 6
Practice 2
REPEAT
OUTPUT "X"
count ← count + 1
UNTIL count = 3
- Output:
X X X
Practice 3
FOR i ← 5 TO 1 STEP -2
OUTPUT i
NEXT i
- Output:
5 3 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
One-Line Dry-Run Rules To Memorise
- FOR → count-controlled, predictable
- WHILE → condition checked first
- REPEAT UNTIL → condition checked last
- Nested → inner completes fully
- STEP → direction matters
- Order of statements matters more than intuition
