12.3.2 Dry Runs, Walkthroughs and Trace Tables
A program does not need to be fully implemented before its logic can be checked. An algorithm can be followed manually using chosen data, while another developer can review its decisions, assumptions and control structures.
Dry runs, walkthroughs and trace tables help developers reason about an algorithm before or alongside computer-based testing. They are particularly useful for locating incorrect variable updates, wrong conditions, skipped data and non-terminating loops.
By the end of this section, you should be able to:
- Explain the purposes of dry runs and walkthroughs.
- Distinguish a dry run from a collaborative walkthrough.
- Construct a trace table for an algorithm.
- Follow assignments, selections and loops in the correct order.
- Record variables, conditions, array values and outputs accurately.
- Use a trace to identify the first point at which behaviour becomes incorrect.
- Suggest a correction and repeat the relevant trace.
Checking Logic Without Running the Program
Manual examination can begin as soon as an algorithm has been written. It does not require a completed executable program.
| Purpose | How manual checking helps |
|---|---|
| Check calculations | Expected values can be calculated independently and compared with the algorithmβs results. |
| Check control flow | The reviewer can confirm which branch or loop iteration should execute. |
| Check initialisation | Variables that begin with an incorrect or missing value can be identified. |
| Check boundaries | Values at or near a condition boundary can be followed carefully. |
| Check termination | The reviewer can determine whether a loop condition can eventually become false. |
| Check requirements | A walkthrough can compare each part of the algorithm with the intended behaviour. |
Dry-Running an Algorithm
During a dry run, the reviewer acts like the processor. No statement should be skipped merely because its effect appears obvious.
A systematic dry-run process
- Choose the input data. Select values that exercise the behaviour being investigated.
- Record the initial state. Include initial variable values before the first repeated step.
- Follow statements in execution order. Pay attention to jumps caused by selections and loops.
- Evaluate each condition. Record whether it is true or false.
- Update values immediately. A later statement must use the new value produced by an earlier assignment.
- Record each loop cycle. Start a new row or group of rows for each iteration.
- Record outputs. Compare them with independently calculated expectations.
- Identify the first divergence. Find the earliest point at which the actual trace differs from the intended trace.
Program Walkthroughs
A walkthrough is usually collaborative. The author explains the design while reviewers ask questions, follow important paths and record possible faults or ambiguities.
Possible walkthrough roles
| Role | Responsibility |
|---|---|
| Author or presenter | Explains the intended behaviour and guides the group through the algorithm. |
| Reviewer | Questions assumptions, follows paths and compares the algorithm with the requirements. |
| Recorder | Documents identified issues, unanswered questions and proposed follow-up work. |
| Facilitator | Keeps the review focused on finding issues rather than immediately rewriting the entire solution. |
What reviewers may examine
- Does the algorithm implement every requirement?
- Are identifiers and module purposes clear?
- Are variables initialised before use?
- Do selection conditions use the correct comparisons?
- Do loops process every intended item?
- Can repetition terminate?
- Are invalid or unusual situations considered?
- Do procedure calls and parameter values match their interfaces?
Dry Run, Walkthrough or Trace Table?
| Feature | Dry run | Walkthrough | Trace table |
|---|---|---|---|
| What it is | A manual execution method | A structured review method | A recording format |
| Typical participants | One developer or reviewer | Author and one or more reviewers | Used by whoever performs the dry run |
| Main focus | Values and control flow for chosen data | Correctness, clarity, assumptions and requirements | Values, conditions, lines and outputs |
| Must sample data be used? | Yes | Often useful, but the review can be broader | Normally records a particular data set |
| Typical result | A sequence of calculated states | A list of issues and actions | A visible record of the manual execution |
Trace Tables
The table should contain enough information to reconstruct the path through the algorithm. It should not contain every identifier automatically.
| Possible column | When it is useful |
|---|---|
| Line number | When the algorithm is long or moves repeatedly between several statements. |
| Loop variable | To identify the current iteration. |
| Current array element | When an array is processed one item at a time. |
| Accumulator | To record a running total or combined result. |
| Counter | To record how often a condition has been satisfied. |
| Boolean condition | To show why a branch was or was not taken. |
| Maximum or minimum | To track a best-so-far value. |
| Output | To record values or messages produced by the algorithm. |
Choosing Useful Trace-Table Columns
Begin by identifying values that change or influence control flow.
Usually include
- variables changed by assignments;
- loop-control variables;
- array elements used in the current iteration;
- conditions that decide whether statements execute;
- outputs or return values;
- line numbers when they improve clarity.
Usually omit
- constants that never change, unless the comparison needs to be visible;
- variables unrelated to the path being traced;
- long expressions that can be represented by one condition column;
- descriptive text that does not help reconstruct execution.
| Algorithm feature | Useful trace columns |
|---|---|
| Running total | Current item and total |
| Conditional counter | Condition result and counter |
| Maximum search | Current item, comparison and current maximum |
| Nested loop | Both loop variables and relevant changing values |
| Procedure call | Arguments, changed reference parameters and return value |
When Should a New Row Be Added?
There is no single trace-table format for every algorithm. A new row is normally used whenever a significant part of the program state changes.
Common choices include:
- one row for the initial values;
- one row for each loop iteration;
- one row for each important assignment;
- one row whenever a condition is evaluated;
- one row whenever output is produced.
Blank cells and unchanged values
Some trace-table conventions repeat the current value in every row. Others leave a cell blank when the value has not changed. Either approach can work when used consistently and when the meaning of a blank cell is clear.
Conditions
Write condition results as TRUE or FALSE. Do not update
statements inside a branch when its condition is false.
Arrays
When an algorithm changes an array, record the element that changed or show the whole array when several positions must be followed. Recopying a large unchanged array on every row may make the table harder to read.
Worked Example: Greenhouse Moisture Summary
A greenhouse records five soil-moisture readings. The algorithm must:
- calculate the total of the five readings;
- count readings below 12, which require urgent watering;
- find the highest reading.
The test data is:
Moisture = [18, 7, 24, 11, 15]
Numbered pseudocode
01 Total β 0
02 UrgentCount β 0
03 Highest β Moisture[1]
04 FOR Index β 1 TO 5
05 Total β Total + Moisture[Index]
06 IF Moisture[Index] < 12 THEN
07 UrgentCount β UrgentCount + 1
08 ENDIF
09 IF Moisture[Index] > Highest THEN
10 Highest β Moisture[Index]
11 ENDIF
12 NEXT Index
13 OUTPUT Total, UrgentCount, Highest
Predict the expected result first
| Result | Independent calculation | Expected value |
|---|---|---|
| Total | 18 + 7 + 24 + 11 + 15 |
75 |
| Urgent count | Values below 12 are 7 and 11 | 2 |
| Highest | Largest value in the array | 24 |
Completed trace table
| Step | Lines | Index | Moisture[Index] | Total | Reading < 12? | UrgentCount | Reading > Highest? | Highest | Output |
|---|---|---|---|---|---|---|---|---|---|
| Initialise | 01β03 | β | β | 0 | β | 0 | β | 18 | β |
| Iteration 1 | 04β12 | 1 | 18 | 18 | FALSE | 0 | FALSE | 18 | β |
| Iteration 2 | 04β12 | 2 | 7 | 25 | TRUE | 1 | FALSE | 18 | β |
| Iteration 3 | 04β12 | 3 | 24 | 49 | FALSE | 1 | TRUE | 24 | β |
| Iteration 4 | 04β12 | 4 | 11 | 60 | TRUE | 2 | FALSE | 24 | β |
| Iteration 5 | 04β12 | 5 | 15 | 75 | FALSE | 2 | FALSE | 24 | β |
| Output | 13 | β | β | 75 | β | 2 | β | 24 | 75, 2, 24 |
The traced output matches the independently predicted result. This increases confidence in the algorithm for this data, but it does not prove that every possible case is correct.
Using a Dry Run to Find the First Divergence
Consider a faulty version of the loop:
FOR Index β 2 TO 5
Total β Total + Moisture[Index]
...
NEXT Index
The requirement says that all five readings must be included, but the first iteration uses index 2.
Faulty trace
| Iteration | Index | Moisture[Index] | Total | Expected total after this point |
|---|---|---|---|---|
| Initial | β | β | 0 | 0 |
| First executed cycle | 2 | 7 | 7 | 25 |
| Next cycle | 3 | 24 | 31 | 49 |
| Next cycle | 4 | 11 | 42 | 60 |
| Final cycle | 5 | 15 | 57 | 75 |
The first divergence occurs when the loop begins with
Index = 2. The reading at position 1 is never added.
The root correction is:
FOR Index β 1 TO 5
Walkthrough Example: Reviewing the Faulty Loop
A review team is given the requirement, the array bounds and the faulty pseudocode. The author explains that the program should process every moisture reading.
| Review stage | Reviewer question | Finding | Action |
|---|---|---|---|
| Confirm requirement | How many readings must contribute to the total? | All five readings | Record the required index range as 1 to 5. |
| Inspect initialisation | Are the accumulators ready before repetition? |
Total and UrgentCount begin at 0.
|
No issue recorded. |
| Inspect loop header | Which array element is visited first? | The loop starts at position 2. | Record that position 1 is skipped. |
| Check consequence | Which results depend on the skipped value? | The total is incorrect; other results may appear correct by coincidence. | Mark this as a logic fault. |
| Agree correction | What is the smallest root-cause correction? | Change the lower bound from 2 to 1. | Amend one statement. |
| Plan verification | How will the group check the correction? | Repeat the trace with the same data and other relevant arrays. | Add regression tests. |
The walkthrough can expose the mismatch without completing every arithmetic step. A dry run then provides numerical evidence of the consequence.
Checking the Quality of a Manual Review
| Check | Question | Weak practice |
|---|---|---|
| Suitable data | Does the input exercise the relevant feature? | Using only values that avoid every selection branch |
| Correct order | Were statements followed in their execution order? | Updating a loop variable before its current iteration finishes |
| Initial values | Were variables recorded before use? | Beginning the table after the first update |
| Condition accuracy | Were Boolean expressions evaluated using current values? | Assuming a branch without calculating its condition |
| Complete iterations | Was every relevant loop cycle represented? | Combining several cycles into one unexplained row |
| Independent expectation | Was the required result calculated separately? | Copying the traced result and calling it expected |
| Clear evidence | Can another person reconstruct the trace? | Using blanks with inconsistent meanings |
| Follow-up | Was the correction checked? | Stopping after identifying the fault |
Interactive: Dry-Run and Walkthrough Lab
Use the widget to build the correct trace, investigate the faulty loop and follow a collaborative walkthrough. Each step reveals the evidence used to reach the conclusion.
Common Mistakes and Misconceptions
- Reading instead of executing. A dry run requires values and decisions to be calculated in order.
- Starting without initial values. The first update cannot be checked if the starting state is missing.
- Using one row for several unexplained iterations. Each loop cycle should be visible.
- Updating a false branch. Statements inside the branch execute only when its condition is true.
- Using future values too early. Every statement uses the current state at that point in execution.
- Choosing too few columns. A missing condition or counter can make the route impossible to reconstruct.
- Choosing every possible column. Unnecessary information can obscure the values that matter.
- Confusing a walkthrough with a dry run. A walkthrough is a collaborative review; a dry run manually executes chosen data.
- Stopping at the final wrong answer. Locate the earliest incorrect update or path.
- Failing to repeat the trace. The corrected algorithm should be checked using the original case and related data.
Practice
Core questions
- Define the term dry run.
- Define the term walkthrough.
- Explain the purpose of a trace table.
- Distinguish a walkthrough from a dry run.
- Explain why initial variable values should be recorded.
- Explain when a Boolean-condition column is useful.
- Explain why expected output should be calculated independently.
- Explain what is meant by the first divergence.
Scenario A: Community Bicycle Returns
LateCount β 0
LongestDelay β Delays[1]
FOR Position β 1 TO 4
IF Delays[Position] > 8 THEN
LateCount β LateCount + 1
ENDIF
IF Delays[Position] > LongestDelay THEN
LongestDelay β Delays[Position]
ENDIF
NEXT Position
OUTPUT LateCount, LongestDelay
Use Delays = [4, 13, 9, 6].
- Predict the expected output.
- Choose suitable trace-table columns.
- Complete the trace table.
- State the final output.
- Explain what would happen if the loop ended at position 3.
Scenario B: Wildlife Camera Review
Accepted β 0
TotalConfidence β 0
FOR Index β 1 TO 5
TotalConfidence β TotalConfidence + Confidence[Index]
IF Confidence[Index] >= 80 THEN
Accepted β Accepted + 1
ENDIF
NEXT Index
Average β TotalConfidence / 4
OUTPUT Accepted, Average
Use Confidence = [92, 76, 84, 65, 88].
- Perform a dry run.
- Identify the first point at which the traced result becomes incorrect.
- Explain the root cause.
- Correct the algorithm.
- Repeat the relevant part of the trace.
Scenario C: Walkthrough Checklist
A procedure should process all six temperature readings, reject values outside the range β20 to 60 and return the number of accepted readings.
- Write five questions a reviewer should ask during a walkthrough.
- Identify the information the author should provide before the review.
- Suggest two useful data sets for a short manual trace.
- Explain what the recorder should document.
- Explain what should happen after a fault is corrected.
Review
| Prompt | A strong response should include |
|---|---|
| Dry run | Manual statement-by-statement execution using selected data. |
| Walkthrough | A structured review in which the author and reviewers examine the solution. |
| Trace table | A record of changing values, conditions, iterations and outputs. |
| Useful columns | Variables that change, loop controls, conditions, array values and outputs. |
| Loop tracing | A separate visible record for each relevant iteration. |
| Finding a fault | Compare expected and actual states to locate the first divergence. |
| After correction | Repeat the original trace and check related cases. |