Pseudocode reference
Trace tables and dry runs
Track how variables change, pass by pass, to catch logic errors before the examiner does.
Why dry-run a program
A program can compile and still give the wrong answer. A dry run means following the program by hand with sample values and writing down each variable after it changes. In an exam, a quick trace catches most logic mistakes.
Trace a factorial loop
This program works out N factorial. Give each changing variable its own column and add a row for every pass of the loop.
DECLARE N : INTEGER
DECLARE Fact : INTEGER
DECLARE I : INTEGER
INPUT N
Fact <- 1
FOR I <- 1 TO N
Fact <- Fact * I
NEXT I
OUTPUT Fact| I | Fact before | Fact after Fact <- Fact * I |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 6 |
| 4 | 6 | 24 |
| 5 | 24 | 120 |
When the trace and the output disagree
Run the program with the same input you traced. If the output is different, check the loop bounds and the starting values first: starting Fact at 0, or looping from 0, are the usual causes.
On a desktop or laptop, flowchart mode replays the run block by block, which lets you compare each step with your table.