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're going to be learning how to use a set of repeated commands.
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 "Using loops in a programme." And by the end of today's lesson, you'll be able to use count-controlled loops.
We'll be using these keywords throughout today's lesson.
Let's take a look at them.
The first word is repeat.
Repeat.
This means happening multiple times.
The next word is command.
Command.
This is a single instruction that can be used in a programme to control a computer.
The next 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.
This lesson is split into two sections.
In the first section, we'll look at identifying repetition.
And in the second section, we'll modify code to draw a shape.
Let's get started with the first section.
I need to finish my dinner if I want to have dessert.
I have five forkfuls left.
What do I need to do to complete this task? Pause the video and have a quick think.
What steps do I need to take to finish my dinner? Which instructions will be repeated? Pause the video and have a think.
If you have five forkfuls left, do you need to repeat any of your actions? Pause the video to have a think.
How many times will they be repeated? Pause the video and have a try.
Well done.
Let's take a look at the answer.
The first step could be put food on the fork.
The second step could be put fork in mouth.
The next step could be eat food.
The steps repeat five times.
If you put your food on your fork and put your fork in your mouth and eat that five times, then you'll be able to take five forkfuls.
You drop 10 pencils on the floor.
You need to pick up all 10 pencils and put them back into the pot.
How would you write a list of instructions for how to do this? Pause the video and have a quick think.
What steps would you need to take to pick up those 10 pencils from the floor? Think about which steps are repeated and how many times you need to repeat them.
This is called repetition.
The first step could be pick up pencil.
The second step could be put in pot.
The steps repeat 10 times.
Pick up pencil, put in pot, repeat 10 times.
That way, you'll be able to pick up 10 pencils.
I wonder what the steps would be if you could pick up two pencils at a time.
If you could pick up two pencils every time, how many times would you need to repeat that? Well, that would be repeat five times.
If you could pick up all 10 in one go, you wouldn't need to repeat the action at all.
I need your help.
I've got a question here and I'd like you to give it a try.
Which is an example of repetition? Is it A, writing a story in your journal, is it B, writing your address on an envelope, or is it C, writing out a word five times to practise spelling? So repetition means to repeat an action again and again.
Which one is an example of repetition? Is it A, B, or C? Pause the video and have a quick try.
Fantastic thinking.
The answer is C.
Writing out a word five times to practise spelling is an example of repetition.
This sequence of code is from Scratch, a block-based programme that could be used to create projects.
What might this code snippet do? Pause the video and have a little look at the code snippet.
See if you can work out what it does.
I noticed there's a repeat block, and the repeat block says four.
This might be repeating four times.
There's a move block that is 50 steps and a turn block.
This turn block is 90 degrees clockwise.
I wonder what this code snippet might do.
This is Aisha, and Aisha says, "The sprite will move 50 steps, then turn 90 degrees clockwise." Lucas says, "These commands will repeat four times." Imagine you place a pencil on some paper and follow these instructions.
What shape will the sequence produce? We've got the same blocks here from Scratch.
If I move 50 steps forwards and then turn 90 degrees, and repeat that for four times, what kind of shape might I get? Pause the video and have a quick think.
This code will produce a square.
That's because each line has the same value of 50 and each angle has a 90-degree turn or a right angle.
Logo is a text-based programming language.
You use Logo to type commands that are then drawn on the screen.
Can you see here? I have a screenshot of Logo.
The command I have here is fd 100, forward 100 steps.
Look at this Logo text-based code and Scratch block-based code.
How are the code sequences similar? Pause the video and have a quick think.
Is there anything that looks the same or anything that looks different? Both sequences use a repeat loop.
The block-based language in Scratch has a repeat four and the text-based code in Logo has a repeat four.
Both sequences repeat four times.
The commands to move 50 and turn right are also the same.
You can look for similar parts of code sequences.
They can help you read and understand unfamiliar code.
Repeat four.
That's the same.
Forwards 50 is the same as move 50 steps.
rt 90 means right turn 90 or turn 90 degrees clockwise.
I have a task here for you.
You've been doing so well listening.
I'd like you to imagine you're giving instructions to draw a square on paper.
Write a clear set of instructions that can be followed step-by-step to draw a square.
Your square should have four equal sides and four right-angled corners.
Test your instructions to see if they are accurate.
Pause the video to finish the task.
I'd like to give you some feedback.
Repeat four times.
Move forward 15 centimetres, then turn right 90 degrees.
This will create a square, where each side has the value of 15 centimetres.
I have another task here for you.
I'd like you to use Logo code to help you complete the Scratch code for the same shapes.
The first one's been done for you.
Here's a square.
The Logo code is repeat four, forwards, fd 50, and rt 90, and can you see the brackets? The brackets are letting us know that those steps need to be repeated four times.
The block-based programming language Scratch says repeat four, move 50 steps, turn right 90 degrees.
The repeat four, the yellow block surrounds both of the blue blocks.
That lets us know that both of those steps are repeated.
Pause the video to finish the task.
Let's look at the triangle.
Let's look at the text-based language in Logo.
Repeat three, open brackets, fd, forwards 50, rt, right turn 120, close brackets.
And those brackets are letting us know that both the forwards and the right turn commands are being repeated three times.
Let's look at the block-based programming language, Scratch.
Repeat three, move 50 steps, turn 120 degrees clockwise.
And both of the blue blocks are within the yellow repeat block, as both commands move and turn need repeating.
Let's look at the hexagon.
Repeat six, open brackets, fd, forwards 50, rt, right turn 60, close brackets.
Let's look at the code in Scratch.
Repeat six, move 50 steps, turn 60 degrees clockwise.
Let's look at the octagon.
Repeat eight, open brackets, fd 50, rt 45, close brackets.
Let's look at the code in Scratch.
Repeat with value of eight, move 50 steps, turn 45 degrees clockwise.
Let's look at the decagon.
Repeat 10, open brackets, fd 50, rt 36, close brackets.
Let's look at the code in Scratch.
Repeat 10, move 50 steps, turn right, 36 degrees clockwise.
You've done so well creating those shapes of count-controlled loops.
Let's now move on to the second section.
Modify code to draw a shape.
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.
Open the project "Draw a triangle" on this link, oak.
link/triangle.
When you open the link, this is what you'll see.
I have a sprite and it's a dog, and looks like he's wearing this space helmet.
He's called sprite one.
I have some blocks all ready in my programming area.
At the top, you've got several blocks.
You need to arrange these blocks and change the value so that the sprite draws a triangle when you press the T.
So you've got the repeat block, you have the move block.
You can choose how many steps you would like your sprite to move.
You have the turn block and you have the pen down block.
So you have to put those together so that your sprite draws a triangle.
If you're not sure how many degrees your sprite should turn to draw a triangle, I'll give you a clue, it's 120 degrees.
That's the turn of the exterior angle of the triangle.
All the commands to create a triangle have already been added to this sprite.
In Scratch, programmes need an event block to trigger them to start.
Here are some of the event blocks you might know.
When green flag is clicked, when space key is pressed, and when this sprite is clicked.
These events trigger the programme to start in different ways.
In this project, the when the T key pressed event block is used to start the programme.
Connect the repeat block to the event block.
This value sets how many times the sequence repeats.
I wonder how many times you'd like your sequence to repeat to make a triangle.
For square, it repeated four times.
For a triangle, I think it might need to repeat less, perhaps three times.
Connect the motion blocks inside the repeat block.
Lucas says, "This value sets how long the side of the shape is." And Aisha says, "If your shape is too small, make this value bigger." Alex says, "This value sets how far the sprite turns." The turn block in Scratch uses degrees to set how far the sprite turns.
All sprites start pointing 90 degrees, which is facing right.
So here's my ladybird sprite.
My ladybird sprite starts off facing right, pointing at 90 degrees.
I can turn my ladybird by 15 degrees each time and my ladybird will spin around on the spot.
True or false? Changing the value in the repeat block changes the shape's size.
Pause the video and have a quick think.
Is that true or false? The answer is false.
Why is that? The repeat block sets the number of sides, how many times the sprite moves and turns.
The move x steps value controls the side.
So this means that if you add more steps to your value, you'll get a bigger shape.
For example, 100 steps.
But if you add less steps to your value, you'll have a smaller shape.
For example, 10 steps.
What does the number three mean in this count-controlled loop? Have a little look at the commands here.
See if you can work out what the number three is doing.
Is it A, how long to draw the line, is it B, how long to wait to start the programme, or is it C, how many times to repeat the code in the loop? Pause the video and have a quick think.
Is that A, B, or C? Yeah, it's the C, how many times to repeat the code in the loop.
To get the sprite to draw the shape when it moves, add the pen down block above the repeat loop.
I have a task here for you.
You're doing so well.
I'd like you to open the project "Create a triangle" at the link, oak.
link/triangle.
Rearrange the coding blocks to programme a triangle.
Add the values in the blocks so that the programme works.
Work out the turn angle.
It's 360 divided by three.
Press the T key to check that your triangle code works.
If you're not sure what 360 divided by three is, it's 120.
Pause the video and have a go of the task.
I'd like to give you some feedback.
Code to draw a triangle.
This code here will draw a triangle.
When letter T key is pressed, put the pen down, then repeat three times, move 100 steps, and turn 120 degrees.
These commands will produce a triangle.
I have another task here for you.
I'd like you to modify code to draw a shape.
Number two, add code sequences to your project that draw two other shapes.
For each shape, use the first letter of the shape name in the event block to start the programme.
For example, when H key is pressed, draw a hexagon.
Work out the turn angle.
For example, the turn angle for a hexagon is 360 divided by six.
You're doing fantastically Pause the video to have a go at the task.
Well done.
You've done so well.
I'd like to give you some feedback.
Number two, code to draw shapes.
Let's look at the code for the square.
When S key is pressed, pen down, we've got the repeat block of a value of four, move 100 steps, turn 90 degrees clockwise.
This will create a square, where every side has the value of 100 steps.
Let's look at the hexagon.
When H key is pressed, pen down, repeat six, move 100 steps, turn right 60 degrees clockwise.
Number two, solution, oak.
link/shape-example.
If you'd like to have a look at how these shapes could be made, go to the link.
You've done so well using count-controlled loops.
Let's summarise.
Using loops in a programme.
In programming, repeated actions can be written as a set of commands.
A count-controlled loop repeats a set of commands a specific number of times.
Using the repeat block in Scratch, you can create shapes by repeating movement and turn commands.
The turn value controls the angle in the shape and the repeat value controls how many sides the shape has.