Lesson video

In progress...

Loading...

Hello, my name is Mr. Hall and thank you for joining me today for this lesson which is "Count-Controlled Loops" taken from the unit repetition in programmes.

I'm really looking forward to learning with you today.

So let's get started.

The outcome of today's lesson is that you'll be able to modify a count-controlled loop to produce a given outcome.

We have three keywords in this lesson.

The first keyword is value.

A value is a number that is used in a command.

Then we have count-controlled loop.

A count-controlled loop is a chunk of code that repeatedly runs a section of the programme a certain number of times.

And finally, we have trace.

And trace means to carefully follow a programme line by line to predict what it will do.

So there's our keywords value, count-controlled loop and trace.

In this lesson, we have two learning cycles.

The first learning cycle which we're going to begin with is to identify the effect of changing values and then we're going to move on to change values to modify a loop.

So let's get started with that first learning outcome.

Lucas is building a brick wall from orange and blue bricks.

He adds four bricks of each colour, but the wall is only half as high as it needs to be.

How many times in total does Lucas need to repeat the blue and orange bricks to get all the way to the top? We can write this question in another way.

So he's already added four times orange and blue blocks, and that's got him halfway.

So we could write that as, 4 x [orange, blue] = halfway.

So that means something times orange and blue will get him all the way to the top.

This is similar to how we show repetition in the text-based programming language, Logo.

The number four before the loop tells us how many times to repeat the loop and then the loop we want to repeat is inside the brackets.

So to double the height of the bricks, the value needs to be doubled.

So the value for half way was four, so if we double four we get to eight.

So he needs to repeat eight times orange, blue blocks to get all the way to the top.

This is an example of how a count-controlled loop is written in Logo.

So we've got two examples, one showing four times repeated and one showing eight times repeated.

So the instruction that Lucas used at the bottom you've seen in the previous slide.

So for a repeat of four times, the code in Logo will be repeat 4 [fd 100 rt 90].

And to repeat eight times in Logo, it's the same code, but instead of saying four for the value, we say eight.

So there's an example of how a count-controlled loop is written in Logo.

A count-controlled loop is a loop that repeats a set number of times.

Let's break down the example you can see on the screen.

So first of all we have the command.

And the command is repeat.

Then we have how many times we want it to repeat? So this time we have four for the number of times we want it to repeat.

And then what we want to repeat is inside the square brackets.

So in this example, we have forward or fd space 100, space rt, space 90 as the instruction we want to repeat.

So the whole line of code is repeat 4 [fd 100 rt 90].

Let's have a look at a question.

What does the number four mean in this count-controlled loop? So the loop we have on the screen is repeat 4 [fd 100 rt 90].

So does the number four mean: A, how long to draw the line? B, how many times to repeat the code in brackets? Or C, how long to wait to start the programme? Well done, the answer is B.

The number four in this example is, how many times to repeat the code in the brackets? Tracing a programme is following a programme line by line to predict what it will do.

When you trace, first you look carefully at each line of code, imagining how the turtle will move.

Then, you draw what you think the result is.

So Aisha and Alex have both used tracing and here are their reflections.

Tracing code helps you check if a programme will work before running it.

If you follow each command step by step and draw the result, you can spot and fix mistakes.

And Alex says tracing also helps you understand how the loops and values affect your shapes, which makes it easier to make changes to your programme.

So Aisha and Alex have both come up with two really good examples of how tracing can help you.

Look at the first command of this programme and the first command we have is fd 100 rt 90.

Think about the code.

Imagine how the turtle will move.

Now, draw the result.

So this would be the result of that code.

So the turtle would move forwards and by default, the turtle will be pointing upwards so it will move up the screen in 100 spaces and then it will right turn by 90 degrees when it gets to the top of the line.

So that's why the arrow is pointing to the right so you should have a straight line going up with the arrow at the top of the turtle at the top with the arrow pointing to the right.

Now look at the second line of code, this will run after the first line so the second line is fd 100 rt 90.

Think about this code.

Imagine how the turtle will move now.

Draw the result.

So now you have a second line which moves to the right of the screen, the same number of spaces as the first line and once again, the turtle has rotated right by 90 degrees so now the turtle is pointing downwards.

The third line of code is fd 100 rt 90.

Think about the code again.

How will the turtle move now? Once again draw the result.

So now you should have a line going back down to be level with the first line that you drew and the turtle should be pointing to the left.

And finally, the last line of code is fd 100 rt 90.

What have you drawn? Well done, you've drawn a square.

So what you've seen is that command repeated four times to draw four lines which makes your square.

And here's all the code in one place.

So four repeats of fd 100 rt 90.

If we wrote this code to make a square using a count-controlled loop, our code would look a bit different.

And it would look like this, repeat 4 [fd 100 rt 90].

Time for a true or false question.

The 100 value in this code will control how far to turn.

The code we have is repeat 4 [fd 100 rt 90].

So the 100 value in this code will control how far to turn.

Is that true or false? Well done, the answer is false.

But can you explain why? That's right, the 100 value is part of fd space 100, which is how far the turtle will move forwards.

Have a go at tracing this programme.

Read the code and draw the result.

What shape will this code draw? Then test the code in Logo.

So the code I want you to trace is repeat 3 [fd 100 rt 120].

The diagram on the left will help you understand what 120 will mean in terms of a turn angle.

So watch this animation to see what that programme would do.

So the line of code it's being typed into the bottom, repeat 3 [fd 100 rt 120].

Now going to execute that and you can see that the turtle has drawn a triangle.

So I'll just let that run through so you can see that once more.

So repeat three times then the instruction we want to repeat forward 100, right turn 120 and there is the triangle.

So now, you're going to change values to modify a loop.

Look closely at these shapes.

There's a square with an angle of 90 degrees underneath and a triangle with an angle of 120 degrees underneath.

The angle shown is the amount of turn needed for each corner of the shape.

Look at the programmes for the square and the triangle.

So the code for a square is repeat 4 [fd 100 rt 90] and the code for a triangle is repeat 3 [fd 100 rt 120].

What other differences and similarities in the two lines of code? So Alex has spotted that the square code has repeat four as the square has four sides and Aisha has identified that a triangle has a three for the repeat because triangles only have three sides.

So there's repeat four for the square and repeat three for the triangle.

Alex has spotted that fd 100 is the same for all sides of the shape, so they are all the same length.

But Aisha has spotted that the turn values are different, as squares and triangles have different angles.

So for the square, we have rt 90, whereas for the triangle, we have rt 120.

What if you wanted to programme a pentagon? So for a square, we had an angle of 90 degrees.

A triangle, the angle was 120 degrees.

For a pentagon, the angle is 72 degrees.

So for a pentagon, you would need to use a different angle value.

Think about the steps for a count-controlled loop.

So Jacob says a count-controlled loop starts with a repeat.

So for our pentagon we would begin with repeat.

Jacob's also spotted that the pentagon has five sides.

So this time we'll need to repeat five times.

So now we have repeat space five.

Let's have a look at the true or false question.

A high number as the repeat value always makes a large shape.

Is that true or false? Well done, it's false.

Can you explain why? The repeat value affects the number of sides, not the size of the shape.

So now Jacob has identified that we need square brackets to show that the loop is starting.

So now our code has repeat 5 and an open square bracket.

Then he's added fd space 100.

So fd space 100 comes first to draw a forward line like the other shapes.

The turn will be the angle of a pentagon which is 72 degrees.

So now the code is repeat 5 [fd 100 rt 72].

And he's remembered to close the square brackets at the end.

So there is the whole line of code, repeat 5 [rd 100 rt 72].

Let's see if you can spot the error in some code.

What is the mistake in this programme for drawing a square? So the code we have is repeat 4 [fd 100 rt 100].

What's the error in that code for drawing a square? Well done, it's the right turn part.

So rt 100 is the wrong angle.

The angle value is incorrect.

The angle for a square is 90.

Now work out the code required for these shapes and then test your programmes in Logo.

So there's three shapes, we have a hexagon where the angle you need is 60 degrees.

An octagon where the angle you need is 45 degrees and a decagon where the angle you need is 36 degrees.

Here's the code you need, for a hexagon it's repeat 6 [fd 100 rt 60].

For an octagon, it's repeat 8 [fd 100 rt 45].

And then finally, for a decagon, repeat 10 [fd 100 rt 36], so there's the code for a hexagon, an octagon and a decagon.

Now use one of your completed shape programmes and make changes to your programme so that it makes the shape larger, makes the shape smaller or changes it into another shape.

So for a hexagon, if you wanted to make the shape larger, you could change the code of repeat 6 [fd 100 rt 60] by changing the fd value for the length of the line.

So you could change it to fd 220.

If you wanted to make it smaller, you would change the fd value to a smaller number.

So in the bottom line of code the fd value has been changed to 50.

To change it to another shape, you would need to change the number of repeats and the angle of the turn.

So in this example, it's been changed to repeat 10 times and the angle has been changed to 36 and that would draw a decagon.

Let's look at a summary of today's lesson.

A count-controlled loop repeats a set number of commands.

Using loops makes programming easier and more efficient.

A count-controlled loop can be used to create shapes without writing each step separately.

Changing the value in the loop lets you change the size or type of shape.

You can trace code by following it line by line to predict what it will do or draw.

Well done and thank you for joining me for today's lesson.

It's been great learning with you today and I look forward to seeing you again soon.