Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 13

Y11-02-CT7: Trace tables

Y11-02-CT7: Trace tables

Learning objectives

In this lesson you will learn to:


• Determine the value of variables using trace
tables

For more information on this topic and additional student activities


refer to Topic 1.8 in the student book.

© Pearson Education Ltd 2021. Copying permitted for purchasing institution only.
Y11-02-CT7: Trace tables

Identifying errors

So far, you have been able to use your development environment to


help you find and fix errors in your code and to show that your code
functions predictably.

Programmers use other techniques as well.

Believe it or not, they even use techniques that only require paper and
pencil.

Today, you’re going to learn how to use one such technique, namely a
trace table.

© Pearson Education Ltd 2021. Copying permitted for purchasing institution only.
Y11-02-CT7: Trace tables

Worked example
• Draw a table with a column for each variable and one for the
output.
• Add blank rows. You can always add more, if required.
num1 num2 count output

© Pearson Education Ltd 2021. Copying permitted for purchasing institution only.
Y11-02-CT7: Trace tables

Worked example
• Start at line 1 and work your way down to line 4.
• Execute the line in your head and fill in the table.
• It is customary to put all initialisations on one line.
• Thereafter, each line of code becomes a line in the trace table.
• Blank lines mean no change. num1 num2 count output
0 0
5

© Pearson Education Ltd 2021. Copying permitted for purchasing institution only.
Y11-02-CT7: Trace tables

Worked example
• Line 5 is a for loop. The first value of count is 0.
• Line 6 does a calculation and changes the value of num2.
• Go back to line 5, to check if count is outside range 3. It is not, so
value of count goes up.
num1 num2 count output
• Line 6 calculates num2 again.
0 0
5 0
0
1
5

© Pearson Education Ltd 2021. Copying permitted for purchasing institution only.
Y11-02-CT7: Trace tables

Worked example
• Go back to line 5, to check if count is outside range 3. It is not, so
value of count goes up.
• Line 6 calculates num2 again.
num1 num2 count output
0 0
5
0
0
1
5
2
10

© Pearson Education Ltd 2021. Copying permitted for purchasing institution only.
Y11-02-CT7: Trace tables

Worked example
• Go back to line 5. The value of count would be 3, so is out of
range. Come out of loop and continue with line 8.
• Line 8 prints a value to the display, so fill in the output column.
• There are no more lines, so you are finished.
num1 num2 count output
0 0
5
0
0
1
5
2
10
10
© Pearson Education Ltd 2021. Copying permitted for purchasing institution only.
Y11-02-CT7: Trace tables

Determine the purpose


• Sometimes, a programmer will have to maintain another programmer’s
code.

• The first step is to try to figure out what the code actually does.

• You can determine the purpose of an algorithm by using a trace table.

© Pearson Education Ltd 2021. Copying permitted for purchasing institution only.
Y11-02-CT7: Trace tables

Check predictable behaviour


• You can demonstrate that code is behaving in a predictable way using a trace table.

• For example, by using test data and a trace table you can show your algorithm
works, even before writing the code.

• This will save you time and effort.

© Pearson Education Ltd 2021. Copying permitted for purchasing institution only.
Y11-02-CT7: Trace tables

Finding and correcting errors

• The most common use of trace tables is to locate errors.

• Remember, this can be done even before the algorithm is written in code on
a computer.

© Pearson Education Ltd 2021. Copying permitted for purchasing institution only.
Y11-02-CT7: Trace tables

Why would you use a trace table?

• Identify and locate errors in algorithms

• Show that an algorithm behaves appropriately given an input

• Determine the purpose of an algorithm.

© Pearson Education Ltd 2021. Copying permitted for purchasing institution only.
Y11-02-CT7: Trace tables

Wrap up: you have learned how to…

 Use trace tables


• Create a table with a column for each
• Fill in a row for each line of code executed

 Identify the purpose of using trace tables


• Determine the purpose of an algorithm
• Show that an algorithm behaves appropriately
• Find and fix errors in algorithms.

© Pearson Education Ltd 2021. Copying permitted for purchasing institution only.

You might also like