Loading...
Hello, my name is Ms. Powell, and welcome to Computing.
I'm so pleased that you decided to join me here today.
In today's lesson, we'll be discussing the difference between count-controlled loops and infinite loops.
Let's get started.
Hello, and welcome to today's lesson from the unit Using repetition in programming to create a game.
Today's lesson is called "Types of loops," and today, we'll be learning to tell the difference between count-controlled loops and infinite loops.
Let's get started.
We'll be using these keywords throughout today's lesson.
The first word is repeat.
Repeat.
This means happen multiple times.
The second word is count-controlled loop.
Count-controlled loop: this is a chunk of code that runs a section of the programme a certain number of times.
And the third word is infinite loop.
Infinite loop: this is a chunk of code that runs a section of the programme continuously.
This lesson is split into two sections.
In the first section we'll compare two types of loops, and in the second section we'll use loops for a purpose.
Let's get started with the first section, compare two types of loops.
In programming, a loop is a command that tells the computer to run the same coding sequence more than once.
Loops are used when the same action needs to be run repeatedly without writing them multiple times.
This makes programmes shorter, clearer, and easier to change.
When we have commands that we want to repeat a set number of times, we use a count-controlled loop.
This is how a count-controlled loop looks in Scratch.
Here you can see the yellow block.
It is the count-controlled loop block, and you can see it says repeat.
There's a space there to put a number in.
You can choose how many times you'd like this block to repeat.
The value controls how many times the sequence inside is repeated, whatever number you'd like to put in there.
In Scratch, there are two main types of loops.
Each one controls how long the instructions are repeated.
There is a count-controlled loop just like the one we've seen.
There's another loop called an infinite loop.
How many times will this sequence repeat? Take a little look here at the blocks from Scratch.
See if you can work out how many times it will repeat.
I can see we've got a repeat block, the yellow block.
The value is five.
I can see a move 10 steps block.
I can see a say block.
This one says, say hello for two seconds.
How many times do you think this sequence will repeat? Is it A, two times; B, five times; or C, 10 times? Pause the video and have a quick think.
Well done for having a try.
Did you get B, five? I can see the value there in the repeat block.
It's repeat five.
That lets me know that this sequence will be repeating five times.
What does the word infinite mean? Pause the video and have a quick think if you know or have heard that word before.
Something that is infinite has no limit or end.
What kinds of things could be described as infinite? Pause the video and have a quick think.
What kinds of things could be described as infinite? Aisha says, "The number of stars in the sky is infinite.
No matter how long you look, you will always find more." Jun says, "Breathing in and out feels infinite.
I keep doing it all day and never stop." Lucas says, "Counting is infinite.
You can keep adding one forever." Programmers can use an infinite loop.
In an infinite loop, commands are repeated over and over again without an endpoint.
In Scratch, the infinite loop is called the repeat forever block.
This is what it looks like here in yellow.
It says forever.
A forever loop is useful for when an action should continue, like a sprite blinking, flapping its wings, or walking in place.
In this example, the infinite loop causes the sprite to keep changing to the next costume continuously.
Can you see the blocks here on the right? I have the infinite loop, it says forever.
And within that loop, it says next costume.
This is the change costume block, so this means the sprite, if I use this sequence, the sprite would change its costume, change its appearance continuously.
True or false? An infinite loop repeats forever.
Pause the video and have a think, is that true or false? The answer's true.
Why is that? An infinite loop repeats forever unless the programme is stopped.
Well done.
You're doing really well.
Compare a count-controlled loop and an infinite loop.
I've got a table here, and we're going to compare the difference between a count-controlled loop, that's one that runs for a set number of times, and an infinite loop, that's one that repeats without stopping.
Let's look at the count-controlled loop.
It repeats a set number of times and good for running a specific sequence.
And this is how it looks in Scratch, it says repeat and you can add a value into the circle.
An infinite loop repeats without stopping.
It's good for ongoing actions.
And you can add your blocks within both the count-controlled and infinite loops.
Which block will create an infinite loop? Is it A, B, or C? Pause the video and have a look at the blocks.
The answer is A.
Well done for having a try.
I have a task here for you.
I'd like you to give it a try.
Trigger the sequences to run by clicking on them.
To find each sequence, you do need to click on each sprite.
What actions are the dinosaurs performing? How many times do they repeat these actions? Record your answers.
Pause the video and have a little look at the link, "Dancing dinosaurs." See if you can answer those questions.
I've got some feedback for you.
Actions the dinosaur is performing: the green dinosaur is looking left and right.
How many times does it repeat the actions? It repeats the actions two times.
The green dinosaur is using a count-controlled loop.
The blue dinosaur.
Actions the dinosaur is performing: the dinosaur is changing costumes, so it looks like it's moving.
How many times does it repeat the actions? It repeats the actions forever.
The blue dinosaur is using an infinite loop.
The red dinosaur, the actions the dinosaur is performing is jumping up and down and changing colour.
How many times does it repeat the actions? It jumps up and down forever, and it changes colour three times.
The sequence of commands for jumping up and down is controlled by an infinite loop, whereas the changing colour happens only three times, so that action is performed by a count-controlled loop.
Well done.
You've done so well in the first section, compare two types of loops.
Let's move on to the second section, use loops for a purpose.
A count-controlled loop repeats a fixed number of times.
An infinite loop repeats without stopping.
Choosing the right loop depends on how long the action should continue.
Be careful when you use infinite loops.
Aisha says, "An infinite loop can get stuck and keep repeating forever." Lucas says, "The only way to stop an infinite loop is to stop the whole programme." This is Alex, and Alex says, "I want my dinosaur sprite to grow a bit bigger and pause before it starts dancing." Which loop should Alex use and why? Should he use the infinite loop or the count-controlled loop? Let's take a little look at what Alex wants.
He wants his dinosaur sprite to get bigger and pause before it starts dancing.
If we look at the sequence of commands for the infinite loop, we have the forever block, change size by 10, and wait one second.
This will keep going continuously.
It looks to me as if the dinosaur will be getting bigger by 10, pausing, getting bigger by 10 and pausing, and that will keep on happening.
In the count-controlled loop, we've got a repeat of two.
It's going to change size by 10, wait by one second, and that's going to repeat two times.
And then the change of size will stop.
Which one do you think is the better loop? Pause the video and have a quick think.
I'd like to give you some feedback.
The count-controlled loop would be more suitable.
An infinite loop would mean the sprite would keep getting bigger until it's too big to see properly.
Even though it would be quite interesting to use the infinite loop and see what happens, the sprite would get bigger and bigger and bigger and it wouldn't stop growing.
However, this is not what Alex wants.
Alex wanted the sprite to get a bit bigger and then stop growing and start dancing, so the count-controlled loop would be much better in this situation.
This is Jun.
Jun says, "I want to make my bird sprite fly backwards and forwards across the top of the screen." Which loop should Jun use and why? Let's take a look at the loops.
We've got the infinite loop, it says forever, and then we've got move 10 steps.
If on edge, bounce.
And that's going to keep going continuously.
The count-controlled loop says repeat 10 times.
Move 10 steps.
If on edge, bounce.
And that's going to happen 10 times and then stop.
Which one do you think would suit Jun better? He wants to make the bird fly backwards and forwards across the top of the screen.
Pause the video and have a quick think.
Would that be the infinite loop or the count-controlled loop? Fantastic thinking.
An infinite loop would be more suitable.
It would keep the bird flying.
A a count-controlled loop would mean that the bird would fly a short way and then stop.
Use loops for a purpose.
In the dinosaur programme, some of the dinosaurs are supposed to repeat their actions forever.
The code for the green dinosaur needs to be modified to also repeat forever.
Modifying means making changes.
This is the code for the green dinosaur.
What would you need to change to get this dinosaur to repeat its sequence forever? Have a little look here at the code for the green dinosaur.
Try to think what might need changing so that it will keep its sequence forever and keep dancing.
We've got repeat two times.
That's a count-controlled loop.
We have point in direction 90.
Wait one second.
Point in direction -90.
Wait one second.
And that's repeated two times.
If I wanted that sequence to repeat forever, what might I change? Pause the video and have a quick think.
Aisha says, "Replace the count-controlled loop with an infinite loop." By removing the count-controlled loop and replacing it with an infinite loop, the dinosaur repeats the sequence forever.
So I'm going to swap my count-controlled loop for an infinite loop.
My dinosaur is now going to be doing that sequence continuously, and it'll appear as if the dinosaur is dancing until the programme is stopped.
I really need your help to fill in the blanks.
I've got some missing words here.
In programming, you use a blank when you want to blank a set of instructions a specific number of times.
You use an blank when you want the instructions to blank until the programme is stopped.
Use the words here in the box below, count-controlled loop, repeat, and infinite loop, to fill in the blanks.
You might want to use one of the words more than once.
Well done for having a try.
Let's look at the answers together.
In programming, you use a count-controlled loop when you want to repeat a set of instructions a specific number of times.
Use an infinite loop when you want the instructions to repeat until the programme is stopped.
Have a task here for you.
I'd like you to give it a try.
Open the project "Dancing dinosaurs" at oak.
link/dinos.
Modify the dinosaur code.
Number one: the green dinosaur should keep looking left and right, but it stops after a few times.
It should look left and right continuously while the programme is running.
Pause the video to finish the task.
Here's the answer.
Change the count-controlled loop to an infinite loop.
So we're removing the count-controlled loop that says repeat, and we're replacing it with the forever block.
This means that the sequence of commands will be repeated continuously for the green dinosaur.
I have another task here for you.
You're doing so well.
I'd like you to modify the dinosaur code.
Number two: the red dinosaur should change colour as it jumps, waiting one second between changes.
It should do this continuously.
Pause the video to see if you can change the code.
I'd like to give you some feedback.
Change the count-controlled loop to an infinite loop.
So you can see here I need to remove the repeat block, and I'm going to swap it for the forever block.
This is what the code should look like.
This means that our dinosaur is going to change colour continuously.
You're doing so well, and I have another task here for you.
I'd like you to modify the dinosaur code.
Number three: the blue dinosaur should change its costume 10 times, then stop.
Pause the video and have a try.
See if you can change the code.
I'd like to give you some feedback.
Change the infinite loop to a count-controlled loop.
In this case, we want it to repeat 10 times and then it will stop.
Open the project "Dancing dinosaurs feedback" that's on oak.
link/dinos-example.
And you'll be able to see all of the correct code.
Well done.
You've done so well in today's lesson.
Let's summarise types of loops.
There are two types of loops used in programming: the count-controlled loop and the infinite loop.
A count-controlled loop repeats a set of instructions a specific number of times, while an infinite loop continues to repeat the instructions without stopping.
You can choose a suitable type of loop for different programming tasks.