video

Lesson video

In progress...

Loading...

Hi, I'm Rebecca your Computing teacher for the Programming Part 3 unit.

For this lesson, you're going to need a Repl.

it account, which you should have already set up with your parent or carer's permission.

You're also going to need a pen and paper to answer the questions that I give you in this lesson.

It's really good idea as well, to try and remove as many distractions as you can so that you can really focus in this lesson.

Once you've got all of that ready, we can begin.

In this lesson, you will use a trace table to walkthrough code that you uses a while loop, and you'll use a trace table to detect and correct errors in programmes.

Let's see what you can remember from lesson one then about while loops.

So what will be output on the screen for the user when this code is executed? Take a look at those different options and pause the video while you have a think about that.

Let's look at the answer then.

So the answer was 3,2,1,0.

So you might have done 4,3,2,1 thinking that it was going to display that 4 straight away.

But actually if you started to mentally trace this in your head, you'll see that you've got count is 4 at the beginning.

And then the while condition says count !=0 which it is.

And then next line of code straight away is count = count - 1.

So now, the value of count is going to be 3 before it is displayed as output.

So the first time it's going to display as 3, then it's going to go from 3,2,1 down to 0.

Now let's just walkthrough this code, just to really see what it's actually doing.

So first line, the state of count is 4, and then you've got, while count !=0 so the first time this executes and it gets to that condition, it is True.

And then you - 1 from the value held by count, and then it displays the value of count as output.

And then it'll go back to that condition and see if it's True or False in this case it's still True because it's 3.

Then it goes down to 2 and it prints that displays as output.

So it's saying 2 this time, and then again goes back to that condition.

To see if it's True or False, it's still True.

So we've got that at 2, and then it's going to - 1.

And now there's only 1 in the variable state for count, and then it prints that.

And then it's still True because 1 is being held in count and count is currently ! = 0.

So it's going to run that line of code again.

It's going to - 1, it's going to display it.

And it's important to know that the condition is only evaluated at the beginning of the loop, and after all instructions within the loop have been executed.

And this is why the loop has not terminated at this instruction.

It's really important to notice this, the condition is not constantly evaluated.

So we still going to keep going.

So then we've got print so that's going to be displaying in as output.

So it is now 0, and then it goes back to the top.

So it doesn't know that it's True or False yet.

It has to go back to the top, to look at that condition again and to evaluate it as either True or False.

So it knows where to go next.

In this situation it's False, so those blocker statements now can no longer be executed.

The loop is terminated and it's going to print.

Now this can be quite tricky to keep track of, and you might have started to think, oh this is quite hard to keep track of and actually walking through code and trying to trace the state of those variables is a really, really good skill to develop as a programmer and walking through code and checking the state of the variables is a great tool for understanding the code and for detecting and correcting errors as well.

And if you remember in lesson one, I was mentally tracing through that programme to change the code from guess is less than four to guess is less than three so that I can see exactly what was going wrong with it.

To help us so that we don't have to keep it all in our heads, we can actually use something called a trace table, and you can see an example of a trace table there on the right hand side.

A trace table allows us to formally record the state of variables, the outputs and the condition evaluations as we mentally execute the code.

So we are still executing it, but we're executing in our heads, not in our Pope Python Programmes.

So here's a walkthrough to show how it works.

So a learner has written a while loop that should stop running once a value count reaches 10.

When the code is executed, the loop never breaks.

There is a logic error.

This is how a trace table can help detect the error in this programme.

And I just moved my camera so that you can see the whole trace table.

So this is a blank one at the moment, let's start adding things into it.

So if you look there in the headings at the top, the variable that we've got there is count and the condition we've got is count ! = 10.

Then you start walking through the code.

So when we walkthrough the code.

The first line has no variables, conditions or outputs so we don't add this to the table.

Now we're on line 2.

So that gets put into the line column.

And then the variable is 4.

So we know that the state of the variable is 4.

That's quite an easy one that first one.

Then nothing else happens.

So there's no conditions in that point, but now there are.

So we've moved on to line 3 and we're looking at that condition this time.

So no line 3, the condition count ! = 10 is True.

So that's what we write there.

And then we look at the next one.

So line 4, line 4 just has some output on it.

So we've called that in the output column.

Line 5 has variable assignment.

So count + 4 refers to the existing value of count on the table and this value is retrieved.

The expression is evaluated and then the value of count is replaced by the new value.

Line 6 has no variables, conditions or outputs.

So we don't add this to the table.

And then as we've reached the end of the while block, we go back and check the condition again.

And line 3's condition is still True so we record this.

Line 4 produces an output so we record this on the table.

Line 5 has variable assignment so we record this.

Line 6 has nothing to record.

So we just leave it off.

And as we have reached the end of the while block we go back and check the condition again.

Line 3's is condition is still True so we record this.

Line 4 produces an output so record this again.

Line 5 has variable assignment so we record this and it is this point where we start to see what is going wrong with the loop.

Because we want this loop to terminate when it's != 10.

So the condition for the loop is count !=10.

The trace table has shown us that count is never equal to 10 and this is why the loop never breaks.

It just runs from eight to 12.

So never equals 10.

So it's never going to work.

So what should the condition be if we want the loop to stop at 10? Pause the video while you have a think about that.

Let's take a look.

So the loop should only continue when the value of count is less than or equal to 10.

So this condition will now work correctly.

You're going to have a go at using your own trace tables now.

Now, if you're able to print the worksheet, then you can do now.

You're going to have a go at doing your own trace tables now, but you'll have to draw out using your pen and paper.

So complete the worksheet to practise using trace tables.

And I'll go through those solutions when you're done.

Pause the video now, while you have a go.

Excellent, let's look at the solutions then.

So this was the solution to the first trace table.

Pause the video while you check if you're right or not.

Great and this is a solution at number two, for trace table 2, pause the video while you check your work.

Excellent, so this is for trace table 3 now.

So pause the video while you mark your work.

And then finally, you had to detect the error and this is what trace table 4 was for.

And you can pause the video while you mark your work.

And the very last bit was to rewrite the code so that it worked correctly.

And that was the solution to that.

So you can pause the video while you check that too.

Excellent, so that was an introduction into trace tables, and hopefully you've seen there how useful they can be.

And they're brilliant for executing the code mentally and looking at the states of variables and also for helping you detect errors in your programmes and fix them.

And if you'd like to please ask your parent or carer to share your work on Instagram, Facebook, or Twitter tagging @OakNational and #LearnwithOak and I'll see you again soon for lesson three.