- [Craig] In this video, we take a look at a really important skill which can help you spot errors,
especially logic errors, in your programs, and this is trace tables.
(uplifting piano jingle)
So, this vital skill can help you understand how a program works.
It is a great way to spot logic errors and test the accuracy of an algorithm and its underlying logic.
It typically involves examining a printed extract of code
and then running through the program as if you were the computer.
You would take each line one at a time and write out in a trace table the current state of each variable.
You would note down any output the program produces as it happens.
Every variable in the program would have its own column in the trace table,
and you would add a new row under any column if the state of that variable changed.
This can all sounds a little abstract,
so let's work through a simple program and use a trace table to figure out what it's doing.
So first, we start by identifying any variables in the program,
and we put each one into its own column to create our trace table.
So, we can see we've got a variable called number, so that goes into a column.
We've got a variable called counter, so that goes into another column.
And we've got a variable called total, so that goes into a third column.
We've also popped an output window down here, where we're going to show any output as it occurs,
and you'll note down the bottom there, it says when the first line of code executes,
number = print("Enter a number")
we're going to assume the user enters the integer 5.
So, we're going to work through this code line by line, as if we were the computer executing this program.
So, the first line of code executes, and the user enters a 5.
So, the contents of the variable number has changed; it now contains 5.
Whenever this happens, you update the row for that variable in the trace table,
so number now holds 5, and of course, we've updated our output window.
The next line is total becomes equal to 1, so we've updated that in our trace table.
For counter equals 1, so we've initialised counter to 1, and you can see we've updated that in our trace table, to number.
So, we enter our FOR loop; total equals total, which is 1, times counter, which is 1, or 1 times 1 is 1.
And that figure goes into total, overwriting what was there.
So, the value of total has changed from 1 to 1.
Now, you might not update this in the trace table, but to be really accurate, it's probably a good idea to do that
to show that you know the value of 1 is being replaced by another value of 1.
We then hit the end of the FOR loop, next counter, and this increments the value of counter by 1,
so we've updated our trace table to show that counter has gone from 1 to 2.
We go back to the top of the FOR loop.
Total now equals total, which was 1 times counter, which was 2, or 1 times 2 is 2,
and that gets overwritten and stored back in total, so we've updated our trace table.
We reach the end of the FOR loop, and we increment counter from 2 to 3.
We go back to the start of the FOR loop.
Total now comes equal to the current total, which is 2,
times the value of counter, which is 3, or 2 times 3 is 6,
and that gets written back in total.
And we hit the end of the FOR loop and increment the value of counter from 3 to 4.
Notice we're very carefully stepping through the algorithm methodically,
one line at a time, as if we were the computer program executing.
Every time a variable updates, we change its value in the trace table by adding an extra row.
So, back to the top again, we're doing for counter equals 1 to number.
Well, number is 5 and counter is 4, so we're still going into this FOR loop.
Total becomes equal to the current value of total, that's 6 times counter, which is 4, 6 times 4 is 24,
and this gets placed into total, so we've updated it.
Next counter, so we increment counter to 5.
We go back to the start of the FOR loop, for counter, which is currently 5,
to number, well number is 5, so 5 still equals 5, so this is okay.
we're going to go into the FOR loop one more time.
Total becomes equal to the current value of total, 24, times the value of counter, 5.
24 times 5 is 120, and we update that into the value total.
We increment counter to 6.
We go back to the start of the FOR loop, for counter equals 1 to the value in number.
Well, the value in number is 5, counter is 6, so now we do not execute the FOR loop
and we skip beyond the end of the FOR loop,
and we output the value of total, which currently stands at 120.
So, what is this program actually doing?
Well, having stepped through it line by line, noting down the contents, noting down the output,
noting down the variables as they change, you've probably figured it out.
It's outputting the factorial of the number entered.
Factorial 5 for example is 1 times 2 times 3 times 4 times 5,
which is 120, and it's the value we ended up with.
(uplifting piano jingle)