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 program.

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 program.

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

What will this program output? Can you spot any patterns in the program? That's right, Sam, the program will count to one 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 four, it could be 10, it could be 100.

Okay, we are 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 one, so the next time the loop is repeated, the number is increased by one.

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 program we've seen previously by using iteration to make the sprite count up from one 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 one.

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 one.

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

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're now going to implement count-controlled iteration in a program.

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

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

In programs 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 program, 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 program doesn't output the correct data.

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

That sounds like a really good idea.

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

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 two.

Okay, the sprite is saying two.

So two multiplied by one is two.

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 program only seems to show the first times table.

Oh, brilliant, Sam's managed to fix the program.

He's added a change multiply-by one 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 one block has been added inside the loop.

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

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

Ah, this time, the sprite is counting up in twos, the two 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 programs?" Have a think.

Do you know why? Ah, that's right, Andeep is saying, "Iteration can make a program 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 one 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 minus one, which means it's reducing the variable life by one each time the loop is run.

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

Start by opening the scratch program, oak.

link/10-green-bottles.

Use the Scratch code to make a version of the nursery rhyme "Ten Green Bottles." Start by opening the program and placing the blocks together to play the first verse of the nursery rhyme, then modify the program 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 one, 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 at the start, we're 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 summarize what we have learned today.

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

Using iteration can improve the efficiency of a program.

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.