Trace Tables In todays lesson we will look

Trace Tables In today’s lesson we will look at: • what we mean by a “dry run” • the purpose of a “trace table” • an example of how to use a trace table to test a program

Dry Run • When you create a program, it’s easy to miss something out, or, when counting, to go too far or stop too soon. • Testing with sample data helps to identify such problems. • Sometimes programmers will test their program without actually running them – they will look at their code and work out the values of the variable on each line, or for each repetition of a loop. • Testing in this way is known as a dry run. • The dry run can be based on a flowchart, pseudocode, or the actual code in your chosen programming language.

Trace Table • One of the most common way to record the results of a dry run is in a trace table. • A trace table has a column for each of your variables, and sometimes an additional column for OUTPUT, depending on the purpose of the program. • There will be a row for each line, or iteration, of the code that you want to test – e. g. if a loop repeats ten times, there will be ten rows in the table. • The programmer works out (manually) what the values of the variables should be at each stage and puts the values in the table. • Remember that variables can be strings, Boolean, etc. , so the value won’t always be a number.

Example • Here is a sample program: y = 3 for x = 1 to 4 y = y + x next print y y = 3 for x in range(1, 5) y = y + x print(y) • Here is what the trace table would look like: X Y 1 3 2 4 3 6 4 9 4 13 OUTPUT 13
- Slides: 4