video

Lesson video

In progress...

Loading...

Hello, my name is Mrs. Holborow, and welcome to Computing.

I'm so pleased you've decided to join me for the lesson today.

In today's lesson, we're going to use iteration to repeat commands a set number of times in a programme.

Are you ready to make a start? Welcome to today's lesson from the unit Using Fundamental Programming Constructs in a Block-Based Language.

This lesson is called Count-Controlled Iteration, and by the end of today's lesson, you'll be able to use count-controlled iteration to repeat a sequence of commands a set number of times.

You'll need to have access to Scratch for this lesson, and it would be useful if you have your worksheet ready.

We will be exploring these keywords during today's lesson.

Iteration, iteration, the process of repeating a sequence of instructions.

Count-controlled, count-controlled, a command that repeatedly runs a sequence of instructions a predefined number of times.

Condition-controlled, condition-controlled, a command that repeatedly runs a sequence of instructions until a condition is no longer being met.

There are two parts to today's lesson.

We will start by looking at count-controlled versus condition-controlled iteration.

We'll then move on to implementing count-controlled iteration in a programme.

Let's make a start by looking at the differences between count-controlled and condition-controlled iteration.

What will this programme output? Can you spot any patterns in the programme? That's right, Sam.

The programme will count to 1 to 10.

The say blocks are repeated, but the number changes.

Repeatedly executing instructions is known in computing as iteration.

Can you think of any repetitive tasks that humans might perform? Pause your video here and have a think.

Did you come up with any examples? I've got an example here to share.

Think about if a coach asks you to do press-ups.

What instructions would they need to give you? That's right, Andeep.

They'd need to tell us how many press-ups they want us to do or when they want us to stop.

Okay, let's do a check of your understanding.

Fill in the blank to correctly complete the sentence.

Is the process of repeating a sequence of instructions.

Pause your video here whilst you have a think.

That's right.

Iteration is the process of repeating a sequence of instructions.

Let's have a look now at the difference between count-controlled iteration and condition-controlled iteration.

Count-controlled iteration executes the commands a set number of times.

So in our example with our coach asking us to do the press-ups, they would say, "Do 20 press-ups," and when we'd done 20, we would stop.

In condition-controlled iteration, commands are executed until the condition is no longer being met.

So in our coach and press-ups example, this might be do press-ups until the two-minute timer is up.

Let's do a check.

Which type of iteration executes commands a set number of times? Is it A, count-controlled iteration, or B, condition-controlled iteration? Pause your video here whilst you have a think.

Did you put A? Well done.

Count-controlled iteration executes commands a set number of times.

The repeat block in Scratch is used to carry out count-controlled iteration.

You'll notice here next to the repeat, we've got an empty white box, and what we can do in there is we can put a number in to say how many times we want the code within that block to run.

So it could be 4, it could be 10, it could be 100.

Okay, we're now moving on to the first task from today's lesson.

Which of the following blocks of code will make the Sprite say "1, 2, 3?" Select the correct block of code and justify your answer.

Pause the video here whilst you complete the activity.

Did you put C? Well done.

The correct answer is C.

This is because the number is displayed at the start of each loop, then the number is changed by 1.

So the next time the loop is repeated, the number is increased by 1, and the loop repeats three times.

So it will say 1, 2, and 3.

Okay, now for the next part of Task A.

Create an improved version of the counting programme we've seen previously by using iteration to make the Sprite count up from 1 to 10.

There's a hint here.

You'll need to use the repeat code block.

Pause your video here whilst you complete the activity.

How did you get on? You're doing a fantastic job so far, so well done.

Hopefully, you've created a code block which looks similar to the one that I have on my screen.

So I've got the when green flag clicked event.

I'm then setting the variable number to 1.

I've then got my repeat block with the number 10, so it's going to repeat whatever code is inside the block 10 times.

Then, inside the repeat code block, I'm saying number for one second.

That's going to display whatever value the variable number is holding.

After I've said number, I'm then going to change number by 1.

So each time the loop runs, the number is increased by 1.

Okay, now for the final part of Task A.

Explain the difference between count-controlled and condition-controlled iteration.

Pause your video here whilst you complete the task.

How did you get on? Let's have a look at a sample answer.

Count-controlled iteration will execute or repeat commands a set number of times.

Condition-controlled iteration will execute commands until a condition is no longer being met.

Okay, we are now moving on to the second part of today's lesson.

Well done so far.

You're doing a fantastic job.

So we are now going to implement count-controlled iteration in a programme.

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

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

In programmes with iteration, it is sometimes difficult to spot errors, as you need to keep track of how many times the instructions are executed.

Let's do a quick check.

Fill in the blank to correctly complete the sentence.

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

Pause the video here whilst you have a think.

That's right.

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

This programme, oak.

link/times-table, asks the user which times table they would like to see.

The sprite should then say the times table for them.

Unfortunately, this programme doesn't output the correct data.

Sam is saying he thinks he needs to run the programme to see what the problem is.

That sounds like a really good idea.

Should we go and run the programme? Okay, so I've opened the programme.

Let's press the green flag, and see what happens.

The sprite is asking me, what times table would you like? I'm going to put in 2.

Okay, the sprite is saying 2.

So 2 multiplied by 1 is 2.

Okay, so it's only telling me the first one, and it's not really repeating it for the rest of the times table.

Let's go back to the slide deck and the code, and have a think about what's wrong with the code.

Okay, so Sam has identified, like we did, that the programme only seems to show the first times table.

Oh, brilliant.

Sam's managed to fix the programme.

He's added a change multiply-by 1 inside the repeat block.

This means each time the repeat block is run, the value of the multiplier is updated, and it shows the whole times table.

Should we have a look at the working solution and see how it now works? Okay, so I've opened the code solution, and you can see that the change multiply-by 1 block has been added inside the loop.

Let's run the programme and see how it works.

I'm gonna type in 2 like we did before.

Ah, this time the sprite is counting up in 2s, the 2 times table, all the way up to 20, and then it stops.

That's because we've done the repeat 10 block.

Sam is asking here, "Why do we use iteration in computer programmes?" Have a think.

Do you know why? Ah, that's right.

Andeep is saying iteration can make a programme more efficient.

So rather than having lots and lots of duplicate code blocks, we can just put one code block inside a repeat.

Let's do a check.

Which of the following reduces the variable life by 1 each time the loop is run? Is it A, B, or C? Pause the video here and have a look at the code blocks carefully.

Did you put C? Well done.

C is the correct code block because it's changing life by -1, which means it's reducing the variable life by 1 each time the loop is run.

Okay, time for your final tasks of today's lesson.

Start by opening the Scratch programme, oak.

link/10-green-bottles.

Use the Scratch code to make a version of the nursery rhyme, "Ten Green Bottles." Start by opening the programme and placing the blocks together to play the first verse of the nursery rhyme.

Then, modify the programme, so that it uses iteration to play the full nursery rhyme.

Pause the video here whilst you complete the activity.

How did you get on? Great job.

Here's a sample solution.

So on the left-hand side, I've got the when green flag clicked event, and then I'm calling the subroutine 10_Green_Bottles.

On the right-hand side, I've got the subroutine.

The subroutine starts by setting the variable green bottles to 10.

I'm then switching the backdrop to green bottles.

I've then got a repeat code block with the number 10 inside.

So it's gonna repeat the set of code 10 times.

Inside that repeat block, I'm starting by saying join green bottles, so the variable that's held in green bottles, to the text green bottles standing on the wall, and then waiting for 0.

5 seconds.

I'm then saying the same line again, so joining green bottles with green bottles on the wall, I'm waiting 0.

5 seconds, and then I'm saying if one green bottle should accidentally fall for 2.

5 seconds, and then I'm changing the green bottles variable by 1.

I'm then switching the backdrop, and then I'm saying there'd be the number held by green bottles, green bottles standing on the wall, and then I'm waiting for one second.

So say the value held by green bottles was 10, okay, at the start.

We are then going to go through this bit of code and see what it's going to say.

So it would start by saying 10 green bottles standing on a wall, 10 green bottles standing on a wall, if one green bottle should accidentally fall, then there'd be nine green bottles standing on the wall, because it's taken that one away.

If you didn't quite get the code working correctly, remember you can always pause the video here and make your corrections.

Okay, you've done a great job in today's lesson.

Iteration can sometimes be a bit tricky, but you've done fantastically.

So let's summarise what we have learned today.

Iteration is the process of repeating instructions within a programme loop.

Using iteration can improve the efficiency of a programme.

Count-controlled iteration will repeat a sequence of instructions a set number of times.

Condition-controlled iteration will repeatedly run a sequence of instructions until a condition is met or is no longer being met.

Well done.

I hope to see you again soon.

Bye.