Jack's Revision Hub

A Level and GCSE Computer Science

Trace Table Generator

Paste Python, C# or pseudocode from a trace table question, add any given inputs, and step through the dry run in this online visualiser. Rows are generated in the layout OCR and AQA mark schemes use.

Program

    Trace table

    Trace table

    Paste or type pseudocode, Python or C# in the Program pane, add any given inputs, then select Build table. Choose OCR or AQA to match the table layout in your question.

    Output

    Ctrl + Enter builds the table. ← → step one row, Home and End jump to the first or last row. In the editor, Tab indents; press Esc then Tab to move on. Your program is saved in this browser only.

    How to complete a trace table

    A trace table records the value of each variable as an algorithm runs. Completing one by hand is a dry run: you execute the algorithm one line at a time, in order, and write each value down when it changes. Questions use the command words trace, dry run and complete the trace table.

    1. Read the algorithm and the table you are given. The columns and their order are set by the question. Do not add, remove or rename columns.
    2. List the inputs the question states, in order. Each input statement takes the next value.
    3. Start at line 1. For each line, work out the right-hand side using the current values, then record the result in that variable's column.
    4. Leave a cell blank when a variable has not changed. In the OCR layout each row also records the number of the line that made the change.
    5. Write only real output in the Output column, exactly as the program prints it, including spaces. Do not write x, - or 0 to mean nothing was output.
    6. Stop when the algorithm ends. In pseudocode and Python a loop counter never takes a value past its limit, so there is no row for it; a C# for loop is different. After a break, the loop's remaining iterations do not run.

    OCR and AQA trace table layouts

    The two boards print trace tables differently. The OCR and AQA switch in the generator produces each layout. Both tables below trace the same program.

    total = 0
    for i = 1 to 5
        total = total + i
    next i
    print(total)

    OCR: one row per changing line

    OCR GCSE Paper 2 questions from 2022, 2023 and 2024 give a Line number column. Each row is a line that changed a variable or produced output. Lines that only test a condition, such as while and endwhile, need no row. This program gives 12 rows.

    Completed trace table, OCR layout, one row per line that changes a value or outputs. Blank cells did not change.
    LinetotaliOutput
    10
    21
    31
    22
    33
    23
    36
    24
    310
    25
    315
    515

    AQA: one row per loop iteration

    AQA's teaching guide on trace tables uses one row per pass through a loop, with columns in the order the variables are first used. In nested loops the outer counter shares a row with the first inner pass beneath it. This program gives 7 rows.

    Completed trace table, AQA layout, one row per loop iteration. Blank cells did not change.
    totaliOutput
    0
    11
    32
    63
    104
    155
    15

    Always follow the table the question prints. The OCR J277 sample paper uses a per-iteration table without line numbers, and both boards accept some variation: OCR's 2023 and 2024 mark schemes accept a repeated unchanged value, and AQA's 2023 mark scheme ignores duplicate values on consecutive rows.

    How trace table questions are marked

    Recent OCR GCSE trace table questions are worth 3 or 4 marks: J277/02 June 2022 Q2(d)(ii) [4], June 2023 Q1(d) [3] and June 2024 Q9(b) [4]. Marks are given for correct values in order, and examiners' reports repeat the same errors.

    ErrorHow it is treatedSource
    Wrong or missing line numbersPenalised once, then later rows are marked on follow throughOCR J277 mark schemes, 2022 to 2024
    A variable change and an output from different lines written on one rowCounted as a line number errorOCR J277 mark scheme, 2023
    Writing x, - or 0 when nothing is outputTreated as output, so the mark is lostOCR J277 mark scheme, 2024
    Outputting a value the program never prints, such as a -1 sentinelNamed as a common mistakeOCR examiners' report, 2023
    Wrong spacing in string output"ab" and "a b" are different stringsOCR examiners' report, 2022
    Normal division where the code uses DIVNamed as a common mistakeAQA examiners' report, 2022
    Losing track of output inside a nested loopNamed as a common mistakeAQA examiners' report, 2023
    Carrying on after a breakNamed as a common mistakeAQA teaching guide: trace tables

    OCR's 2024 report advises that candidates "simply leave boxes blank if no output is given on a particular line".

    Trace table examples

    Completed tables for six common algorithms. Each one loads into the generator with the same layout, so you can step through it row by row.

    While loop trace table

    number = 4
    while number < 512
        number = number * 2
    endwhile
    print(number)
    Completed trace table, OCR layout, one row per line that changes a value or outputs. Blank cells did not change.
    LinenumberOutput
    14
    38
    316
    332
    364
    3128
    3256
    3512
    5512

    The condition on line 2 is tested before every pass, but it changes nothing, so it has no row. When number reaches 512 the condition is false and the loop ends.

    Linear search trace table

    array numbers = [7, 12, 3, 16, 9]
    target = 16
    found = false
    for i = 0 to 4
        if numbers[i] == target then
            found = true
            break
        endif
    next i
    Completed trace table, OCR layout, one row per line that changes a value or outputs. Blank cells did not change.
    LinenumberstargetfoundiOutput
    1[7, 12, 3, 16, 9]
    216
    3False
    40
    41
    42
    43
    6True

    The target is at index 3. After break the loop stops, so i never reaches 4. The comparison on line 5 changes nothing and has no row.

    Selection with elseif

    array marks = [72, 45, 58]
    for i = 0 to 2
        if marks[i] >= 70 then
            grade = "A"
        elseif marks[i] >= 50 then
            grade = "B"
        else
            grade = "C"
        endif
        print(marks[i], grade)
    next i
    Completed trace table, OCR layout, one row per line that changes a value or outputs. Blank cells did not change.
    LinemarksigradeOutput
    1[72, 45, 58]
    20
    4A
    1072 A
    21
    8C
    1045 C
    22
    6B
    1058 B

    Only the first true branch runs. print with two items outputs them separated by a space.

    Do until loop with inputs

    total = 0
    count = 0
    do
        mark = int(input("Enter a mark, -1 to stop"))
        if mark != -1 then
            total = total + mark
            count = count + 1
        endif
    until mark == -1
    print("Average", total / count)
    Completed trace table, OCR layout, one row per line that changes a value or outputs. Blank cells did not change.
    LinetotalcountmarkOutput
    10
    20
    412
    612
    71
    47
    619
    72
    45
    624
    73
    4-1
    10Average 8

    Inputs: 12, 7, 5, -1. The body runs before the condition is tested. The sentinel -1 is stored in mark but never added or output.

    Nested loop trace table

    total = 0
    for i = 1 to 3
        for j = 1 to 3
            num = i * j
            total = total + num
        next j
    next i
    print(total)
    Completed trace table, AQA layout, one row per loop iteration. Blank cells did not change.
    totalijnumOutput
    0
    1111
    322
    633
    8212
    1224
    1836
    21313
    2726
    3639
    36

    AQA layout. i appears once per group of three inner passes, on the same row as the first. The OCR layout of this program has one row per changing line.

    Bubble sort trace table

    array numbers = [5, 2, 4, 1]
    n = 4
    for i = 0 to n - 2
        for j = 0 to n - 2
            if numbers[j] > numbers[j + 1] then
                temp = numbers[j]
                numbers[j] = numbers[j + 1]
                numbers[j + 1] = temp
            endif
        next j
    next i
    print(numbers)
    Completed trace table, AQA layout, one row per loop iteration. Blank cells did not change.
    numbersnijtempOutput
    [5, 2, 4, 1]4
    [2, 5, 4, 1]005
    [2, 4, 5, 1]15
    [2, 4, 1, 5]25
    10
    [2, 1, 4, 5]14
    2
    [1, 2, 4, 5]202
    1
    2
    [1, 2, 4, 5]

    AQA layout. A row where only j changes is a comparison with no swap. The array is sorted after the second pass, but this version still completes every pass.

    Python trace tables

    Choose Python in the toolbar, or paste a Python program and the generator switches to Python. The program runs with Python 3's rules, so each row holds the value Python would store, and a program that would crash stops at the same line with the same type of error.

    Python for loop with range()

    total = 0
    for i in range(1, 6):
        total = total + i
    print(total)
    Completed trace table, OCR layout, one row per line that changes a value or outputs. Blank cells did not change.
    LinetotaliOutput
    10
    21
    31
    22
    33
    23
    36
    24
    310
    25
    315
    415

    range(1, 6) stops before 6, so i takes the values 1 to 5 and keeps 5 after the loop. The rows are the same as for the pseudocode for i = 1 to 5, but Python has no next i line, so the print is line 4.

    Python while loop with inputs

    total = 0
    count = 0
    mark = int(input("Enter a mark, -1 to stop: "))
    while mark != -1:
        total = total + mark
        count = count + 1
        mark = int(input("Enter a mark, -1 to stop: "))
    print("Average", total / count)
    Completed trace table, OCR layout, one row per line that changes a value or outputs. Blank cells did not change.
    LinetotalcountmarkOutput
    10
    20
    312
    512
    61
    77
    519
    62
    75
    524
    63
    7-1
    8Average 8.0

    Inputs: 12, 7, 5, -1. input() returns a string, so int() converts each value before it is compared with -1. total / count is 24 / 3, and / always gives a float in Python, so the output is Average 8.0, not Average 8.

    Python function with parameters and return

    def area(width, height):
        result = width * height
        return result
    
    total = 0
    for side in [2, 3, 4]:
        total = total + area(side, side)
    print("Total area:", total)
    Completed trace table, OCR layout, one row per line that changes a value or outputs. Blank cells did not change.
    LinetotalsidewidthheightresultOutput
    50
    62
    122
    24
    74
    63
    133
    29
    713
    64
    144
    216
    729
    8Total area: 29

    Each call sets width and height on line 1, the def line, then runs the function body. The returned value is added to total on line 7. The return line changes no variable, so it has no row.

    What changes when you trace Python

    RuleExampleIn the table
    range(a, b) stops before brange(1, 6) gives 1 to 5No row for 6
    / always gives a float24 / 3 is 8.0Write 8.0, not 8
    // and % round down-7 // 2 is -4 and -7 % 2 is 1Negative results round down, not towards zero
    input() returns a string"5" + 1 is a TypeErrorThe table stops at that line. Use int(input())
    print puts a space between itemsprint("Total", t) gives Total 15print("Total" + str(t)) gives Total15
    end= keeps output on one lineprint(i, end=" ")Each row's Output cell holds the text that call printed
    Lists print with brackets[1, 2, 4, 5] and ['Kofi', 'Ama']Shown as Python prints them
    Parameters are set by the calldef area(width, height):Their values appear on the def line

    The prompt in input("Enter a mark") is not written in the Output column, as in OCR's mark schemes. A variable inside a function shares a column with any variable of the same name outside it. Supported: int, float, str and bool, lists, tuples and dictionaries, slicing, f-strings, for, while, if, elif, else, break, continue, functions including recursion, try and except, and the math module. Not supported: classes, files and the random module, because a random value is different on every run.

    C# trace tables

    Choose C# in the toolbar, or paste a C# program and the generator switches to it. The program is type-checked and run with C#'s rules, so the table holds the values C# would store, a compile error is reported with the compiler's own message, and an exception stops the table at the line that threw it.

    C# for loop

    int total = 0;
    for (int i = 1; i <= 5; i++)
    {
        total = total + i;
    }
    Console.WriteLine(total);
    Completed trace table, OCR layout, one row per line that changes a value or outputs. Blank cells did not change.
    LinetotaliOutput
    10
    21
    41
    22
    43
    23
    46
    24
    410
    25
    415
    26
    615

    In C#, i++ runs before the test i <= 5, so i does become 6 and the test that ends the loop is made with 6. The last row on line 2 records that. A pseudocode for i = 1 to 5 loop never holds 6.

    Recursive method

    static int Factorial(int n)
    {
        if (n <= 1)
        {
            return 1;
        }
        return n * Factorial(n - 1);
    }
    
    int result = Factorial(4);
    Console.WriteLine(result);
    Completed trace table, OCR layout, one row per line that changes a value or outputs. Blank cells did not change.
    LinenresultOutput
    14
    13
    12
    11
    1024
    1124

    Each call sets n on line 1, the method's first line, so the four calls give four rows before any of them returns. Nothing is assigned until the calls unwind and line 10 stores 24 in result.

    What changes when you trace C#

    RuleExampleIn the table
    Dividing two ints gives an int7 / 2 is 3, 7 / 2.0 is 3.5double d = 7 / 2; stores 3, because the division happens first
    A for loop's variable passes its limitfor (int i = 1; i <= 5; i++)A last row with i = 6
    Casting to int cuts off the decimals(int)2.7 is 2Convert.ToInt32(2.7) rounds instead, to 3
    Rounding goes to the even numberMath.Round(2.5) is 2Math.Round(3.5) is 4
    A whole-number double prints without .0Console.WriteLine(3.0) prints 3Write 3, not 3.0
    int wraps round when it overflows2147483647 + 1-2147483648
    Console.Write has no new lineConsole.Write("Name: ")The prompt is output, on its own row
    ref and out change the caller's variableSwap(ref x, ref y)The parameter and the caller's variable change on the same row
    A compile error stops the whole programint x = 3.5;No table, and the compiler's message, such as CS0266

    Programs can be top-level statements or a class with a static Main. Methods printed without their class, as exam papers often show them, are read as the members of one. Supported: int, long, double, float, char, bool and string, arrays including two-dimensional ones, List, Dictionary, Stack and Queue, for, foreach, while, do while, switch, static methods with ref and out, recursion, try and catch, string formatting and int.TryParse. Not supported: classes of your own, properties, LINQ queries, lambdas and Random, because a random value is different on every run.

    Supported pseudocode, Python and C#

    The generator reads OCR Exam Reference Language, AQA pseudo-code, Python 3 and C#. It does not run Java or Visual Basic. Pseudocode keywords are not case sensitive.

    ConstructOCRAQAPythonC#
    Assignmentx = 3x ← 3x = 3 x += 1int x = 3; x++;
    Count-controlled loopfor i = 0 to 9 next iFOR i ← 0 TO 9 ENDFORfor i in range(10):for (int i = 0; i < 10; i++)
    Condition-controlled loopwhile x < 5 endwhileWHILE x < 5 ENDWHILEwhile x < 5:while (x < 5)
    Post-condition loopdo until x == 5REPEAT UNTIL x = 5while True: with breakdo { } while (x != 5);
    Selectionif elseif else endifIF ELSE IF ELSE ENDIFif elif elseif else if else switch
    Outputprint("Total", t)OUTPUT 'Total: ', tprint("Total", t)Console.WriteLine("Total " + t);
    Inputx = input("Prompt")x ← USERINPUTx = input("Prompt")x = Console.ReadLine();
    Comparison== != < <= > >== ≠ < ≤ > ≥== != < <= > >=== != < <= > >=
    Arithmetic+ - * / ^ DIV MOD+ - * / DIV MOD+ - * / // % **+ - * / %
    Arrays and listsarray a = [1, 2] a[0]a ← [1, 2] a[0]a = [1, 2] a.append(3)int[] a = { 1, 2 }; List<int>
    Stringss.length s.upper s.substring(0, 3)LEN(s) SUBSTRING(0, 2, s)len(s) s.upper() s[0:3]s.Length s.ToUpper() s.Substring(0, 3)
    Castingint() str() float()STRING_TO_INT() INT_TO_STRING()int() str() float()int.Parse() (int)x Convert.ToInt32()
    Comments// note# note# note// note
    Leaving a loopNot in ERLBREAK, used in the teaching guidebreak continuebreak continue
    Procedures and functionsprocedure functionSUBROUTINEdef returnstatic int Add(int a, int b)

    Not supported yet: procedures, functions, switch statements and two-dimensional arrays in pseudocode; classes, files and the random module in Python; classes of your own, LINQ queries, lambdas and Random in C#.

    Given inputs are read in order. In pseudocode, values that look like numbers are treated as numbers because questions state inputs as values; in Python, input() returns a string, as it does when the program runs. In the OCR layout the items in print are separated by a space; in the AQA layout the items in OUTPUT are joined directly, so any space must be inside the quotes. Python's print always separates items with a space, and C# joins text only where the program uses +. In C#, Console.ReadLine() returns a string, as Python's input() does. A pseudocode line the generator does not recognise is listed in a warning rather than skipped silently.

    Common questions about trace tables

    Should I leave a cell blank or repeat the value?

    Leave it blank. A blank cell means the value has not changed. OCR's 2023 and 2024 mark schemes and AQA's 2023 mark scheme accept a repeated value, so repeating is not penalised, but blank is the expected layout and is easier to check.

    When do I start a new row?

    In the OCR layout, start a new row for each line that changes a variable or produces output. In the AQA layout, start a new row for each pass through the loop. If the question's table has a Line number column, it expects the OCR layout.

    Is there a row for the loop counter's final value?

    No. for i = 1 to 5 stops at 5, and the counter never holds 6. AQA's pseudo-code guide states the counter never takes a value above its limit. A separate variable that counts something, such as count = count + 1, does keep its final value.

    What goes in the Output column?

    Only what the program prints, exactly as it appears, in the row where it is printed. Leave the cell blank on other rows. OCR's 2024 mark scheme treats any entry, including x, - or 0, as output.

    Can it make a trace table from Python code?

    Yes. Choose Python in the toolbar, or paste a Python program and the generator switches to Python. It follows Python 3's rules: int and float are separate types, so 24 / 3 is 8.0, and input() returns a string. A program that would stop with an error, such as a TypeError or IndexError, stops at the same line, and the table shows the rows up to that point.

    Can it make a trace table from C# code?

    Yes. Choose C# in the toolbar, or paste a C# program and the generator switches to it. The program is type-checked first, so int x = 3.5; gives the compiler's error CS0266 and no table, as it would never run. Then it runs with C#'s rules: 7 / 2 is 3, and a for loop's variable ends one past its limit. Programs can be top-level statements, a class with Main, or methods copied from an exam paper without their class.

    Is this a trace table maker or a checker?

    Both. Paste a program and it generates the completed table, or complete the table by hand first and step through the generated rows to check each one. It is not a blank template: every row comes from running the program.

    Is a trace table the same as a dry run?

    A dry run is working through an algorithm by hand without running it. A trace table is where the values from the dry run are recorded. Questions use both terms.

    Does this help with A Level Computer Science?

    Yes for loops, selection, arrays and string handling. OCR's H446 clarification guide says candidates need to be able to "read, create and trace code (for example using a trace table)". Recent H446 Paper 2 tracing questions also use recursive functions. Those run in Python and C#, where the OCR layout gives one row for each call's parameters, but not yet in pseudocode. AQA A Level Paper 1 is taken on screen in the programming language the school chooses, and C# and Python are both options.

    Trace tables in the specifications

    OCR A Level H446

    Writing and following algorithms (1.2.3) and programming constructs (2.2.1). The subject content clarification guide adds tracing code with a trace table and tracing recursive functions. Revise the algorithms in the Paper 2 notes.

    OCR GCSE J277

    2.1.2 lists trace tables, with the guidance "Create and use trace tables to follow an algorithm". They are assessed in Paper 2.

    AQA GCSE 8525

    3.1.1: "use trace tables and visual inspection to determine how simple algorithms work and what their purpose is".

    AQA A Level 7517

    4.4.1.2: "Be able to hand-trace algorithms". The specification does not name trace tables.

    Sources: OCR H446 clarification guide, OCR J277 specification, OCR J277/02 examiners' reports for 2022, 2023 and 2024, AQA teaching guide: trace tables, AQA pseudo-code guide.