video

Lesson video

In progress...

Loading...

Hello, welcome to lesson five of our programme unit.

I'm Ben and this lesson is going to be all about count-controlled iteration.

So as ever, all you'll need for this lesson is a web browser and access to the Website Scratch.

So if you'd like to save your work, you'll need an account.

And if you're under 13, you'll need to get your parent, carer or teacher to set you up with an account.

So if you can clear away any distractions that you might have, turn your mobile phone off, maybe get some water that you can keep yourself refreshed, but away from the computer.

And then when you've done all that, and you're ready, let's begin.

In this lesson, you will understand the need for iteration and describe the need for iteration as well.

We're going to implement count-controlled iteration.

And then finally we're going to look at how we can detect and correct errors in any programme, okay.

So the first exercise we're going to do is to look at these blocks of code now hopefully by now you fairly familiar with looking at this and maybe trying to understand what it does.

But what I'd like to do is I'd like to work out what the programme is going to output.

And importantly, can you spot any repetition and or any patterns.

Now, I want to see those are two separate things, because repetition and passing can be the same thing.

But here in this instance, you might see a different instance of repetition, and maybe another pattern happening, Okay.

So I'd like to pause this video for a moment, and then ready to study this code and see if you can tell me by the end of it, where the repetition is and where there's any patterns going on, okay.

So I'll continue so when you're ready, unpause it and we'll talk through the answers.

Okay, so let's look at what we've got here.

So hopefully by now, you've worked out that when we click the green flag, it's going to say one for one second, and then two for one second.

So basically, it's going to count to 10.

Leave a second between each time it says something new.

So what Can we see there in terms of repetition? Well, the repetition is the same block being used 10 times.

We can see the same block.

It's, using the same number of seconds for each one, and it's doing it 10 times.

Okay, so that'll be the repetition.

But the pattern you might have spotted there will be that what the, what is being said, is incrementing by one each time.

So there's a very clear pattern there, every time a new save block has been put in, the number is increasing by one.

Okay, so we're going to introduce you to the term iteration.

Now, you may have heard of this as being referred to as a loop.

But it's the same thing but we like to refer to in computing as the term iteration.

So you've heard both terms now, but iteration is in computing is a process of repeatedly executing instructions.

So being able to repeat executed or so repeatedly execute instructions is commonly referred to in computing as iteration.

But like I say, you might have heard the term loops.

And so Can you think of any repetitive tasks that computer or humans might be able to perform.

Okay.

So if you pause that for a second, I mean, there are lots of examples, but pull us out when you've got an example of that.

Then if you're on pause, and we'll talk talk through that a bit more.

Okay, so one example that I mean, there are lots of examples of this, because one of the reasons why computers were invented in the first place was to maybe make humans lives easier and want that thing if humans are doing the same task over and over again.

And what a computer can do is do that activity, do it faster and possibly more accurately, okay.

So one thing examples that I could think of that is that when I was at school, I remember my teachers gave given my, me my report at the end of the year, and that would involve them having to do it by hand.

So a teacher would write their comment, and they would also write maybe the effort grade and natural subject grade, and then they would pass that to the next teacher would then have to write it by hand, and so on and so forth.

So all the teachers eventually pass this piece of paper.

They're taught to write it in it before.

Eventually it's complete and given to me.

Whereas now, all your teachers most likely will write your report on the computer, and the computer software will do a repetitive task of collecting all of your, all the reports from teacher and pop it in that document virtually instantaneously.

Okay, so that's a job that a computer can do using iteration.

So, one another example of a repetitive task is I'd like to think about in a fantasy situation, a fantasy scenario, where you can think about the teacher tells you to write lines in, as a punishment.

I'm sure none of you have ever given any of your teachers any reason to give you any kind of punishment like this.

But So pretend for a second that maybe you misbehave in the computing lesson, which I'm sure would never ever happen, but if it did, and you're teaching So to give you lines, what instructions would they give? Okay, so just think about that for a second.

What instructions might they give you? Okay, so the instructions that I think they'll give you is they will tell you what to write So for example, I must behave in my computing lessons, they would tell you that you need to do it repetitively.

And they would also tell you how many times to do this or when to stop.

So that might be you have to write it maybe 50 times, or there'll be in particularly mean, then they might say that you need to rewrite the lines till four o'clock.

So all those, those three things, there pretty much model how we would tell a computer to repeat a task, we tell it what to do, and we tell it, how many times to do it or when to stop.

So, there are two different types of iteration that we need to look at.

Now, the first one is count-controls, and that's the one we're going to look at in this lesson.

Now count-control is iteration is when you're telling it to execute the commands a set number of times.

So the example is you would write out those lines 100 times or do this task, X amount of times.

But the other type of iteration, like so we're not going to look at that in this lesson is condition controls.

So condition control will execute the commands until the condition you set is no longer being met.

So again, an example of that is using the example of lines is right out the lines until 4pm.

So you'd have to keep going until that condition is no longer being met, because the time is after 4pm.

Okay, so we're going to have a go at implementing this by having making a counting cat programme.

So, let's go back to that first exercise we did right at the beginning of this lesson.

Now, together, we're going to work through the steps of how to add iteration to that.

So we're going to start off with this, code that's going to have 10 blocks sentense say lines in it.

And then what we'll do is make it more elegant.

Now you could, some people would say, what I mean by that is we're going to do it in less lines of code because rather than write out 10 blocks of code, we can say, write this line of code 10 times.

Now some people refer to that As efficiency, we're actually probably a better term for it is elegance.

Okay, If you do something and less lines of code than you need to that will be better known as elegant code.

It's not really fair to say that it's inefficient, It's efficient code, because it will still take the computer the same amount of processing power to process the instructions.

It might be efficient on the programmers time, because it takes them less time to write that many lines of code.

But actually, for the computer, it makes little difference.

So a nice way of referring to that is elegant code.

So we'll let's head over now to the Scratch programme.

And if you can watch what I do, because then I'm going to ask you to repeat the same steps with your Counting cat programme.

So I've headed over to scratch now and this is my counting cat programme.

So watch this demonstration carefully, because I'm going to ask you to go once I finish this, I'm going to ask you to go to your worksheet and have a go at making or repeating the steps that I'm going to show you now.

So right at the beginning of this lesson, we looked at this programme, and we worked out that when we run it it's going to, say, one to 10.

And leave a second between each one.

So we can see some repetition there, and a pattern as well.

So the repetition was, these blocks of beings used 10 times.

But the pattern was that the number is incrementing.

Every time we see a new block, so let's just have a look at that code running.

So I'm going to press the green flag there.

And you can see every second a new number has been said, and it should stop when we get to 10.

Okay, it does perfect.

Now, we talked about a moment ago about the fact we wanted to make code more elegant, which means we're going to put it in less lines of code.

Anytime you see a repetition like this, one of the thoughts that we're wanting to have in your head is I wonder if I could make this more elegant.

I wonder if I can do it in less lines of code.

And often when you see this level of repetition, you probably can.

So what I'm going to do is I'm going to get rid of all but one of my say blocks So on how to do that is click on this number two like I did there, just drag it down.

And then I'm just going to move over here and let go of my mouse and that will delete the blocks.

Now I said that wanted some kind of level of repetition.

So hopefully you notice how many times this programme repeated the same block, okay, can you remember? So it was 10 times it repeated it.

So I'm going to go over to my control panel here.

And I'm going to look at these blocks.

Now if you remember we have used these ones before 'cause these are selection blocks.

So my if statement here these that said that selection and an if else, but we also have some other blocks.

So just take a moment and have a look at the screen now.

Which one of these blocks do you think we're going to use now? Like good hopefully, you spotted that it was the repeat block.

Okay, so I want to repeat that 10 times.

So I'm just going to shove it in there.

Can you see the way I've kind of tried to make it so the block is going to fit that same block is going to fit in, in the middle? There we go.

Now, scratch allows you to change that number,Okay? So I could change it to repeat five times.

Okay, but actually, the default 10 was exactly what I wanted 'cause I wanted it to say one to 10, 10 times, right? So as simple as that I've put my repeat in there.

And I've solved the problem often in way less lines of code.

And when I run it is going to say, the numbers one to 10.

So let's test that.

Right, lets watch.

Okay, so it still stuck on one.

Why is it stuck on one? Is it stuck on one? It's just saying one and that's disappeared.

So I wonder why that was? Can you spot why that was? Now, a common thing for us to think and trust me, I'm one of these people.

I sometimes think it too.

When I've coded that exactly right.

So why is that not working? Remember from our our first lesson, computers will follow out your commands to the letter.

It won't make any assumptions.

So I've done that, I've told it to repeat 10 times and say one for one second, but do that 10 times.

So I'm only ever going to see my cat saying one, I wanted the number to increase every time.

Now let's think back again to the other skills that we've developed out this unit.

Hopefully by now that you might recognise that we can have something called a variable, and we can increase the value with that variable like we did with a score in our in our quiz programme.

So I'm going to head over to my variable section here and you'll notice that I pre made a variable called number Okay, So I could say change this to say number for one second.

Now this is an interesting experiment 'cause nothing will change as such, but let's have a look on over on it.

Right, so number set for some strange reason in my programme is 11.

Okay, which is strange but sometimes by default, the number might be zero, but either way, in my programme it turns out that it's 11, Okay.

But either way, that's not the number that I want.

So right at the beginning of my programme, what number do I want the first number to be? Okay.

Say out loud at the screen.

So three to one and the number needs to be one.

Okay, the first number is one, so I'm going to set my number to one, okay, and there we go.

Let's run that.

Okay, so it's staying one.

So I set my number to one, and it's saying that number for one second, but after it said that number, what do I want to happen? Well, I want to increase that number by one, okay.

So we have this option here.

And we've used this before, it's changed number by one, okay.

And remember, if I've got more than one variable in my programme, I can select it from the drop down list, but I only have one so it by default, it's going to that variable name.

So I'm going to change number by one.

So let's read the through, okay.

When green flag is clicked, it's going to set the number to one and then I want to repeat these instructions 10 times, say the number for one second, and then change that number by one, Okay.

so I'm going to run this and see if it works.

Okay, looking pretty good.

It's just stopped at 10.

Perfect, Okay, now, I talked about making this programme more elegant.

So ignoring that when green flag block our code, remember we had 10 blocks of code, we have 10, say blocks.

Okay, so how many lines of code have I gotten now? Well, I've gotten one.

So one, set number to one, then I've got two, which is my repeat block.

I've got three, which is same number one second, and then four, which is changed number by one.

So I've taken my programme which had 10 lines of code to be four lines of code.

So I've made it much more elegant.

And I've used a count-control loop because my iteration there, the count-controlled iteration is I'm telling it to do the two blocks of code that set number of times.

Okay, so I'm going to head back over to my slides now.

Okay, so it's time to pause the video now.

And then I'd like to go to your worksheet where the instructions for this task and the link to that counting cat programme and on task one of your worksheet.

So see if you can repeat the steps to pause the video, see if you can repeat the steps.

Remember, you can always rewind the video a little bit see if there's some part of that that you've forgotten.

And then once you've made your counting cat programme more elegant, then unpause the video and we'll continue with the lesson.

Okay, so well done on doing that and well done on taking the first steps into using count-controlled iteration.

So the next task is for you to do this by yourself in a slightly different context.

Okay, so I'd like to your task is to use Scratch code to make a version of the nursery rhyme 10 green bottles.

So you're going to go into your worksheets in a second you going to to open the programme and place the blocks together to play the first verse of nursery rhyme so you can see on the screen In here, you're going to begin a programme that's got all the blocks that you need to create the first verse of 10 Green Bottles.

So you need to put them together and then run it to make sure it just does the first verse.

But then here's the kind of the task really, the tricky part is to modify it, so that it then add some iteration to play the full nursery rhyme.

So you'll need to use a count-control loop and you'll need to use variables as well to change some things about it as it goes through the loop.

Okay, so good luck with that.

I hope you enjoy it.

So if you head over to pause the video in a second, head over to your task two where you'll find the link to the programme and the instructions and then restart the video once you've completed that.

Okay, so hopefully you're starting this video because you've completed it, but if you're starting this video, because you're struggling with it, then don't worry.

I'm just going to have it quicker this myself.

And I'll to show you how to do it and then maybe you could pause the video again and go back and complete it yourself.

Okay, so I'm going to start off by minimising this preview window.

So I've got a bit more room to work with my code, I'm just going to move everything to the right place.

So I suddenly started off with all the blocks of code to make the first verse of 10 Green Bottles.

So actually, when we look at it, we've got one green flag clicked, you're going to switch the backdrop to 10 and then we're running this sub-routine 10 Green Bottles.

So the first task was to kind of get 10 green bottles sub-routine working, so it did the first verse, Okay.

So it starts off with 10 green bottles, Danny on the wall.

10 Green Bottles standing on the wall, and then what are the blocks have we got? Okay.

10 Green Bottles standing on the wall, 10 Green Bttles standing on the wall.

And one on one green bottle should accidentally fall.

Oh, Alright, just need to undo something there, there we go.

I accidentally deleted that block of code.

So I'm going to get back.

So I have one Green Bottle should accidentally fall, there'll be nine Green Bottles standing on the wall, okay.

And I've also got these block of both pause points as well that I could use.

And I've also got this switch to backdrop.

Okay, now this switch to backdrop means obviously, what I want to happen is that when one green bottle should accidentally fall, there'll be nine Green Bottles.

So I want my backdrop to actually show nine green bottles, okay.

So I'm just going to pop that here for a second.

Okay, now we've also got these weight blocks that we can use.

So I'm going to use my weight blocks in between says, there may be a break in between saying each one, Okay.

so and then I'm going to put the weight right to the end.

So let's see how that works.

So I'll just make my preview window a little bit bigger and see how that works.

And if one green bottle should accidentally fall, there'll be nine green bottles standing on the wall and that should change to the previous backdrop.

Now actually looking at that, I actually think it'd be better if the backdrop switch to nine before it said nine green bottles standing on the wall.

So I'm just going to pop that there.

Yeah, I think that'd be better.

I'm not going to run it through again.

But just take my word, I think that would look better.

But again, it's not essential, it doesn't really matter too much.

So hopefully now I've got my first version of it.

And notice how when a grid and click the green flag, it's going to reset my backdrop to 10 green bottles, Okay, so there we go.

10 Green Bottles, 10 Green Bottles standing on the wall.

And then one green bottle should accidentally fall switch nine bottles.

There'll be nine green bottles standing on the wall perfect.

Now, the key bit here now is to think about what do we need to change every time Okay, and how many times do you need to repeat, Okay? So I'm actually going to do my repeat but first because I think either 10 Green Bottles standing on the wall and I need to get down to zero.

I'm going to, I'm going to put repeat 10.

Okay, now what do I want to repeat? What pretty much I want to repeat that everything that is under here, okay.

So it's going to go from 10 to nine every time.

So 10 nine, eight, seven, six, five, four, three, two, one.

So what do I how do I get it so that it's going to each time it's going to reduce that number, Okay? So, I'm going to look what variables I've got,Okay, and I've got a green bottles variable, perfect.

Okay, so rather than saying, say 10 Green Bottles, I'm going to say say green bottles every time.

So each time I see 10, I'm going to change it to say, green bottles.

Now, hopefully you'll notice it was saying 10, 10 but this time we're saying nine, but I'm going to put in the green bottles, word here.

So whatever green bottle is now It's just going to say the same thing every time, this isn't going to work perfectly, but let's test it out.

Nine green bottles standing on the wall, nine green bottles standing on the wall.

And if one green bottle should accidentally fall, there'll be nine green bottles standing on the wall, which isn't right as if you started at nine and one bottle fell, the next number should be eight, Okay? So what we need to do is find a way in which we're going to reduce this, the number.

So so far we've used this we use this what this block in the previous exercise change, bottle by one, now we change it that by one, it's going to get up by one, we want to reduce it down by one.

So all we need to do is change this to minus one.

Okay, so, at what point do we want it to minus one? Well imagine this number is 10.

So 10 green bottles standging on the wall, 10 green bottles standnging on the wall and if one green bottle should accidentally fall, we want to then change the bottles by minus one Let's test that out.

You'll notice the green bottle is nine and actually, we want to start off as 10.

So let's deal with that right now as well.

That'd be good.

So when we start off with the first thing when we first run our sub-routine 10 green bottles, maybe at that point, no matter when we run at the first time we run it, before we start the loop, we want the value of green bottles to be 10.

So then, have a look at these options here that we can see on the left hand-side, see right where my mouse is pointing.

Which one of these 20 do we need? Well, the one we need a set green bottles to zero but change the zero to 10.

Perfect, so this going to be perfect.

What do you think? Let's hope so, Okay.

we're going to start off, run the green, the green flag is 10 is there 10 green bottles standing on the wall and if one green bottle should accidentally fall it should go to nine and it does and then nine green bottles standing on the wall.

So next time we run it round nine Green Bottles standing on the wall, nine green bottles standing on the wall.

And if one green bottle should accidentally fall we should have eight, and we do have a green bottle standing on the wall, but we're fine hopefully you're seeing a problem.

And the problem here is that our drinks bottles are still under a nine.

So what can see is switched backdrops and lemmi just show you that 'cause if I click on the backdrops lemmi move my screen a second move my face, okay, if we go to backdrops, I can click on that and go to backdrops.

You can see I've got a lot of backdrops and they're numbered one to nine.

So one is obviously just with one bottle and two is with two bottles.

Okay, so let me move myself back.

We'll go back to my scratch cat.

I called Bob.

Encode, Okay, so we can see, when the game was I, when the programme starts, we switch back backdrop to 10.

And then at this point, here, it switched it to nine.

Well, that's still the same value, of my, you know, of the value of green bottles.

So green bottle starts off with 10.

But then we want to then green bottles changes to nine, Okay? So if I actually change that would it work if I pause, switch back drop to green bottles, because remember, the value of being held, or being stored, be held by that variable, sorry, is nine at that point.

So all I'm doing is saying switch backdrop to the number nine.

Let's see if that works.

So the value of the variable is 10.

So 10 green bottles, 10 green bottles if one should fall, the value of the variable turns nine.

And the costume does change.

So the backdrop does change it goes to nine.

So let's just test that again.

So hopefully, we should see this bottle disappear, it should go to eight.

There we go.

And it does.

So I'm just going to let this run through.

Just to make sure the whole thing works and we actually get to the end.

Seven we done six now you can skip this forward video forward if you want to see you know, by 30 seconds or so until we get down to zero just to make sure it works and hopefully you've done this already.

So like I said once we, once we just waiting for this programme to run through just to double check that it's working.

Just remember that if you want to go back to your previous programme and change anything or edit anything that you've just had differently to make then, please feel free to go ahead and do that.

So down to three green bottles now.

The three green bottles standing on the wall, and if one green bottle should accidentally fall we got tWO green bottles, One green bottle, one green bottle, and hopefully to get down to zero.

There'll be no green bottle standing on the wall, Okay.

I mean supposedly, the slides was not a bug the slight issue with the last one was it says they're zero green bottles standing on the wall which is absolutely fine.

But you might want to say there'll be no green bottles standing on the wall.

So I'm not going to do that now.

But have a think about how you would do that.

Maybe you might put some selection in there with a condition that maybe said if green bottles were zero, then say this, otherwise say this, okay? But have a go that if you want to push yourself a little bit more, okay.

So like say go back and make any changes that you need to and then you, when you're ready, we'll have we'll head back to the slides.

So for the next part of this lesson, we're going to look at computer bugs.

Now bug in a computer system is code that causes your programme to behave unexpectedly.

The first recorded bug in the computer system was part of work done by the famous computer scientist Grace Hopper.

Now, the term computer bug was used to refer to malfunctioning kind of machinery and way before computers even existed.

But actually Grace Hoppers team were working on machine called the Mark Two.

And actually one time it malfunctioned.

And when her team tried to work out why it malfunctioned.

Now she came across a moth in the hardware that actually made it malfunction.

So this mOth here that you can see on the picture there, that's the actual mOth that caused that, that malfunction in the hardware.

So grace actually put it in a notebook because it was part of the running joke that actually a real bug for once had actually caused something to malfunction.

Now computer hardware now is so reliable that actually the term computer bug really refers to software or errors in code that have caused the programme to not run as expected.

So what's important for us Is that we're able to learn techniques where we can solve those problems in our code that might have caused a bug, Okay.

So debugging is the process of finding an error in your code and taking steps to fix the problem.

So the code on the right-hand side that you can see there has a problem that needs debugging.

So I'd like to head over in a second to task three worksheets to find the bug and see if you can find a solution to that bug, Okay? So you going to pause the video and like to, like I say, go over to your task three on your worksheet, where you'll find a link to the scratch programme, that has the bug, you going to spend a little bit of time looking at the code to see if you can work out what that bug is.

And then if you follow the instructions, it will help you solve the problem in your code, Okay? So good luck with that good luck, the problem solving, and as unpause the video when you when you've solve the problem.

Okay, so as always, we're going to just have a look at the answers to the exercise that you've just completed.

So hopefully you were able to complete it, you worked out what the problem was, maybe you've already gotten your mind to how you'll go about solving it.

In fact, you might have even gone and solved it.

Okay, so if we have a look at programme itself again, the first thing to do is to just run through the programme, see what happens.

So I'm going to click the green flag.

And there's going to say, what times table would you like? Well, I wanted to give me the five times table.

So if I type in five and hit enter, you'll see that says five, then nothing else.

Still doing nothing.

But actually, remember, the computer is actually doing exactly what we tell it to.

So it is not doing nothing.

But it's the same problem as our counting cat originally, is that we've got it say number multiplied by, by multiply by for one second, but it's doing that 10 times.

So the fault that I've got it's actually saying the same number 10 times which isn't what I want, Okay.

I wanted table four times table.

So the first kind of problem solving technique we can do, is putting in some say blocks and actually work out what's happening with the value of the variable on each one, the iterations each time that loops, okay.

So if I go to looks and put in a couple of say seconds blocks, okay.

And then I'm going to replace the word hello with the variables that are being used, 'cause I want to know what number is, and what's happening to multiply by.

Okay, so let's have a look.

So if I run this through now, and see that the first time iterate through, so when I type in five, the first time I iterate through, then number is one and multiply, sorry numbers five and multiplies one, the second time number is five, multiply as one, for, So the next time number is five is multiplying by one.

So it says, What's happening here is that my number is staying as five which is what I wanted because it's a five times table, but my multiply by is staying as one.

So what this is highlighting to me is that five times one is five, and it's just going to do that 10 times.

But the five time table actually next time I want it to be five times two, and then five times three, and then five times four, and so on, Okay.

So if I stop that there, I'm just going to remove these blocks.

In fact, no, I'm not going to keep them in, I'm going to solve it and just double check that, that works.

So I'm going to, I want to increment multiply by each time.

So again, I'm going to use my change multiply by, and I'm going to put that there.

So I'm going to change multiply by, by one.

So let's see if that works.

So I'm going to go and use my five times table.

Okay, so it's five one, and therefore that should be five times one is five, then it should say, hopefully five and then two, does say five times two is 10, perfect.

So let, I think that's working now.

So this is the point where I feel comfortable that I can remove these two blocks so that don't want them in the programme.

That's just for my debugging purposes.

So I'm going to pop that back in and let's see if this works.

So five times table I've 10, 15, 20, 25, 30 and I've asked it to repeat 10 times.

So hopefully, we should get to the number 50.

And that's just stop.

And it does perfect, Okay, so that's how we solved our problem.

So that's one of many techniques you can use to debug a programme.

So remember, if you're ever struggling with your work with your programme, and not quite sure what's happening, particularly when we're using variables, then try just pop in some of your say blocks in there and put in the value of the variable so you can track what that variable is doing all the way through the iteration, Okay.

So let's head back over to the slides.

Okay, so we've reached the end of this lesson.

And yeah, again, you've done a really, really great job if you've managed all of these tasks.

I mean, we've learned about count-controlled iteration, we've learned about other types of iteration such as condition controls, we've also learned how to debug our own programmes, which is a really great achievement.

And what would be really nice as well if you were able to write down maybe three of What you think are the most important things that you've learned from this lesson, Okay.

And also, we would love to see the work that you've done.

So please do share it with us.

Please take a screenshot of the work that you've done or might want to take a photograph of you working hard with your code.

And if you'd like to do that, then please ask your parent or carer to share your work with us on Instagram, Facebook or Twitter, tagging @OakNational and #LearnwithOak.

So next lesson is the final lesson in this unit, and I'm looking forward to seeing you then.

Okay, so I'll see you next lesson.

Bye.