Loading...
Hello, my name is Mr. Hogan.
I'm excited to be learning with you today.
We are going to have such a great time learning about programming iteration.
I'll be supporting you with our learning during these lessons.
I'm so pleased you're decided to learn about using trace tables.
We are going to do brilliantly.
So let's have a look at the outcome to the lesson.
So the outcome is I can use trace tables to trace how variables change in a while loop.
This is a really good skill to have, and it would be really good to go through this lesson with you to help and support you.
We've got two keywords for this lesson.
We've got trace table.
So this is a tool used to track the values of variables and the flow of execution in an algorithm or programme, step by step.
We've also got condition.
This is used to control the flow of execution in a programme; a condition contains a logical expression.
So we're gonna be using these throughout the lesson.
And remember, you can always pause or even rewind the video to look at these definitions.
This is the outline of the lesson.
It's split into two learning cycles.
The first one is explain how to trace variable values, and the second one is trace values in a while loop.
This is brilliant.
This is gonna be an amazing lesson.
I hope you really enjoy it.
So let's have a look at the first learning cycle, explain how to trace variable values.
A while loop will repeat actions while a condition is True.
When the condition is False, the while loop will not execute.
The next line of code will run.
So this is represented on the right.
So we have while condition, and then the block of code under that will run.
And then if it's False, the next line of code will run.
This is represented in the diagram as well, where we have a diamond for the decision or the condition.
If it's True, it will veer off to the right-hand side, and some lines of code or blocks of code will be run, and then it'll loop back to the condition.
A trace table can be used to help walk through the steps of an algorithm.
A trace table is like a notebook for tracking how an algorithm or programme works.
A trace table allows you to formally record the state of variables, the outputs, and the condition evaluations as you mentally execute the algorithm.
There are several advantages of using trace tables, such as: providing a clear, step-by-step visual representation of how an algorithm processes data; helping to pinpoint errors in logic or calculations before they could become larger problems in the code; and it also offers a structured way to grasp how algorithms work and how variables change over time.
Some great advantages there.
Let's have a quick check, a true or false one.
Trace tables are helpful for finding logic errors in algorithms. So is that true or false? Remember, you can pause the video at any time or rewind it.
Let's have a look at the answer.
So the answer is true.
Yes, trace tables are very helpful for finding logic errors.
Well done if you got that right.
It's correct because trace tables are used to step through an algorithm, tracking the values of variables, which also helps identify logic errors.
This trace table can be used to record the state of variables, the outputs, and the condition evaluations during a walkthrough for the flowchart on the next slide.
Try to look at the flowchart on the right and start going through, step by step, looking at each row on the trace table and seeing if you can match it to the correct symbol or point of the flowchart.
So on row one, for example, we've got count is set to one.
And therefore, we've got count = 1 in our first symbol.
Then we've got total = 0.
So on our second row, we've then got total = 0.
There's no need to put the one in there for count 'cause you don't update it unless it changes.
And just look through the flow diagram and see if you can pick out where the condition perhaps equals true and what happens after that.
There are many ways to create trace tables.
The exact format, layout, and structure will vary with each algorithm.
Note that although trace tables are useful to spot errors, they can still be subject to human error, especially when tracing complex algorithms. A trace table will contain vertical columns that may represent important information such as the line, variables, conditions, and outputs of an algorithm.
The number of columns needed, as well as the column names, will vary with each algorithm.
A trace table will contain horizontal rows that represent each step in the execution of an algorithm.
The number of rows needed in the trace table will vary, obviously, with each algorithm.
Let's have a quick check.
In a trace table, what does each row typically represent? Is it a, a variable in the algorithm? Is it b, a condition in the algorithm? Or is it c, a step in the algorithm? Remember, have a little think.
You can pause the video anytime or even rewind it.
Should we have a look at the answer? It is c, a step in the algorithm.
It's time for a practise.
One, explain what a trace table is and describe how and why programmes use them.
Remember, you can pause the video anytime or rewind it.
So a possible answer could be: A trace table is used to record and track the execution steps of an algorithm.
A trace table has columns that represent important information such as the line, variables, conditions, and outputs.
The rows in the table represent each line of the algorithm.
The values of the variables in the table are written down with each step so you can see how they change over time.
Trace tables are used because they can help pinpoint areas in logic or calculations before they become larger problems. So that's quite a comprehensive answer there, but hopefully you've picked out some really important parts of it, or maybe you've done really well and covered all of it.
Well done.
So let's move on to the second part of the lesson.
I can't believe it's gone so quickly.
You're doing so well to go through this and learn lots more knowledge about trace tables.
So in this learning cycle, we're going to trace values in a while loop.
A learner has written a while loop that should stop running once the value of count reaches 10.
So you can see that code on the right-hand side of the slide.
sleep(1) pauses the programme for one second between each loop so you can see the output more clearly.
However the code is executed, the loop never breaks.
There is a logic error.
Here is a blank trace table that we're going to use.
We've got line, variable, condition, and output as headers for our columns.
You can identify variables and conditions, and add them to the headings to help you keep track.
So we've added count and the condition count != 10.
Let's have a quick check before we move on.
Code that gives fixed messages such as print("Welcome") is included in trace tables.
Is that true or false? Remember, you can rewind the video at any time to take a look at previous slides, which may have the answer on them, or you can pause it at any time.
Let's have a look at the answer.
It is false.
Why? Well, print statements are part of the program's execution.
If they don't depend on changing variables, they're not used in trace tables to keep them focus on the program's logic.
Well done for getting that right.
If you didn't, don't worry.
I hope you understand why that false is the answer.
So now we're gonna walk through the code.
The first line has no variables, conditions, or output, so you don't add anything to this table.
We are just importing sleep from the time module.
On line two, four is assigned to the count variable, so we record this on the table.
So you put two for line, and the variable count is now four.
Line three has a condition, count != 10.
The condition is evaluated, and you record the value on the table.
So on the second row down there, we've got on line three of our algorithm the condition is now set to True.
Line four produces an output, so you record it on the table.
So you can see now we put on line four our output is four.
Line five has a variable assignment.
count + 4 refers to existing value of count on the table.
This value is retrieved, the expression is evaluated, and then the value of count is replaced by the new value.
So you can see now on line five our variable, count, has increased by four.
So it's now eight.
It doesn't remain at four.
Line six has no variables, conditions, or output, so you don't need to add this to the table.
So we loop back to line three.
So we go back to check the condition again.
Line three's condition is still True, so we record this on the table.
Line four produces an output, so you record this on the table.
So you can see we've put eight as the output.
Line five has a variable assignment, so you record this.
So again, you can see on our table we refer to line five.
And now our count variable has increased by four again.
So it's 4 added to the 8, which is 12.
And line six has nothing to record again.
As you may have reached the end of the while block, go back and check the condition again.
Line three's condition is still True, so we record this.
Line four produces an output.
Line five has a variable assignment, so you can record this.
So we've added four again to count to make 16.
At this point, you can start to see what is going wrong with the loop.
The condition for the loop is count != 10.
The trace table has shown that count is never equal to 10, and this is why the loop never breaks.
So you can see from the greyed out rows on the trace table that we have skipped from variable count being eight to being 12.
Let's have a quick check.
What should the condition be if you want the loop to stop at 10? Is it a, count = 10? Is it b, count is less than or equals to 10? Or is it c, count is greater than or equals 10? The code is on the right for you to look at and to help you.
And also, you can pause the video or rewind it at any time.
Should we have a look at the answer? So it is less than or equals to 10.
The loop should only continue when the value of count is less than or equal to 10.
So this condition will now work correctly.
Let's have a practise.
Create a trace table to trace the values of line, variable, count, and conditions in this programme.
Use the trace table to trace through the code.
So take your time.
Have a look at the code on the right-hand side.
You can always rewind the video and have a look at previous slides to help you as well.
Let's have a look at the answer.
So let's have a look at this code.
So line one doesn't give us any changes in the variable count condition or output, so we don't need to put anything.
Line two, we've got count = 5.
So on our trace table, we've put five in for count, or the variable.
And then on line three, that condition, count != 0, well, that is true.
Count is not equals to 0.
And then we've got the output there on line four, which is five.
We then minus one from the count variable.
So that becomes four now on line five.
So we've put that in our trace table.
And we go back to the condition.
And it's still True, isn't it? So then we go to four as an output on line four.
And then on line five, we go down to three 'cause we minus one of the count variable on line five again.
And then our condition is True.
And we continue to line four, which is output three.
And then we continue.
You can see that, and we can carry on to as soon as that count equals zero, that condition on line three becomes false.
Well done on getting that right.
Really good.
This is the first time you've probably applied your knowledge of trace tables to an algorithm and tracing it.
It's line by line.
Brilliant, well done.
The second question in the practise task.
Create a trace table to trace the values of line, variable, count, and conditions in this programme.
Use the trace table to trace through the code.
Okay, so take a look at that code.
Familiarise yourself with it.
Remember, you can pause the video at any time, or you can rewind it.
Shall we have a look at the answer? So here's the trace table.
Let's have a look at it together.
So on line one, a variable, number, stores the value of two.
On line two, the condition, which is number less than 11, is True.
On line three, the output is two.
And then on line four, we add two to number, so two becomes four.
And then we start the loop again and look at the condition.
So is the condition True or False? So is count less than 11? Well, it is, so it's True.
And then on line three, output becomes four.
And then on line four, the number variable we add two to, so it becomes six.
And then on line two, the condition becomes True.
And we can continue tracing this through the table.
Perhaps you want to just read through this yourself.
So on line three, when the output becomes 10, the next line is line four, which is where we add 2 to 10.
We go back to line two, and the condition, number less than 11, is false.
So then the loop stops.
Well done for getting that right.
Or even if you've got parts of it right, that's amazing.
Well done.
Also, you've reached the end of the lesson.
Well done.
So in summary, a trace table is a tool used to track the values of variables and the flow of execution in a programme, step by step.
Also, trace tables help identify logic errors by showing how variable values and the condition's result change with each step of the programme.
Amazing.
So good.
Well done for going through this lesson and learning all about trace tables and actually using them.