Lesson video

In progress...

Loading...

Hi, my name's Mr. Hall, and welcome to this lesson, "Creating a programme that uses loops," taken from the unit, "Repetition in programmes." I'm delighted that you are here learning with me today, and I really hope you enjoy this lesson.

Let's get started.

The outcome for today's lesson is that you will create a programme using count-controlled loops to produce a given outcome.

With two keywords for this lesson, so the first keyword is procedure and the second is count-controlled loop.

Let's see what they mean.

So a procedure, a procedure is a named set of commands that can be called multiple times throughout a programme.

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.

We've got two learning cycles today in this lesson.

So first of all, you are going to design a programme that uses count-controlled loops, and then you are going to implement a design as a programme.

In this lesson, you'll plan, and then make a programme to design some wrapping paper.

Here's an example of the type of thing you might design by the end of the lesson.

First, you will create a procedure to draw a shape, then you will use that procedure in a pattern.

After that, you will add colour to your pattern.

Your programme must use shapes with a count-controlled loop.

It can contain more than one shape, for example, a square or a triangle.

Your programme must use different colours.

It can use the same shape in different sizes.

For example, a large hexagon could be combined with a small hexagon.

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

Let's have a look at an example of a count-controlled loop.

So in this loop, the command is to repeat.

The repeat is going to run four times, represented by the number four.

And then inside the square brackets, we have what we're going to repeat.

So we're going to repeat fd, which is forward 100, rt, which is right turn 90.

You will use count-controlled loops to create your shapes.

So let's go back through that.

So we have the command, which is repeat.

How many times it's repeated, which is four times.

And what you want to repeat inside the square brackets, so that's forward 100, right turn 90.

So let's have a little check for understanding here.

Which shape will this loop draw? So the code we have is repeat 10, and then in square brackets, forward 100, right turn 36.

So will that draw, A, a hexagon, B, an octagon, or C, a decagon? And by each of those shapes, we have the angle you need to create that shape.

So for a hexagon, it's 60 degrees.

For an octagon, it's 45 degrees.

And for a decagon, it's 36 degrees.

See if you can answer the question.

So which shape will this loop draw? A hexagon, an octagon, or a decagon? Well done.

This loop will draw a decagon, and there's some clues in the code which help us work that out.

So first of all, the number of repeats is 10, and a decagon has 10 sides.

And then the turn inside the brackets is right turn 36.

So each time you turn, you're turning by 36 degrees, which is the angle you need to draw a decagon.

So, 10 turns of 36 degrees will draw a decagon.

So now let's have a look at procedures.

A procedure is a named chunk of code that can be called multiple times.

Creating a procedure will save time later on because you can just run that code chunk by just typing the name you use for that procedure.

So in the picture, we have an example of the procedure square.

So the procedure square has been defined as repeat 4, and then in brackets forward 100, right turn 90.

So to run that code, all you would need to do is type square.

Let's have a look at that animation demonstrating how to create a procedure.

So we're defining it as to square.

And then in to square, we're going to type repeat 4.

And then in brackets, forward 100, right turn 90, close bracket, and click end.

Now if you type in the bottom square and run, you'll create a square.

So that's creating a procedure to draw a square.

So let's have a look how to create a procedure for a square.

So, to square, and then type in repeat 40, square brackets, forward 100, right turn 90, and then end.

So that code in Logo will create your procedure for drawing a square.

So square is the name of the procedure, repeat 4, in brackets, forward 100, right turn 90, is the code to draw a square, and that's all you need.

So, Jacob's identified that a procedure can save you time when you have a long programme, as you can call the procedure just by typing its name.

And Alex adds, "Remember to give your procedure a sensible name, so you remember what it is used for.

The name of the procedure has no meaning to the computer." It's only there to help you.

So this pattern was made by first creating a procedure for a shape.

Then, the procedure was used in a programme that repeated the shape, turning the turtle to draw the shape at different angles.

So first of all, the first procedure draws the shape once, which you can see on the left.

And then if you repeat it a number of times with a turn, you get that shape being drawn multiple times.

And then if you increase the number of the repeats, you can get some really nice detailed patterns.

Your patterns will be made by repeating shapes with turns in between.

So let's have a look at some examples.

So the example on the left is 10 repeats of a triangle with a right turn of 36.

Then we have an example of a hexagon.

So all these have the procedures already written.

So this is repeat 18, hexagon, so that's the procedure, right turn 20.

And then finally, we have square, which is, again, been defined as a procedure.

So repeat eight times, the procedure, square, right turn 40.

So these are all shapes which have been made by repeating procedures with turns in between.

So have a go at this question.

True or false? A count-controlled loop repeats forever.

Well done.

The answer is false.

But can you tell me why? So a count-controlled loop only repeats a specific number of times.

You choose how many times the loop runs, and when it reaches that number it stops.

So that's why a count-controlled loop does not repeat forever, 'cause it stops after a certain number of times.

So let's move on to planning a programme.

Why is it important to plan before you programme? Take a minute to think about that question.

So, Lucas identified that a plan helps you organise your ideas and avoid mistakes.

Jacob said a plan can help you decide the shapes to use, where they go and which colours to use.

And Aisha adds, "It can also make it easier to write and debug your programme." So these are all really great reasons why it's important to plan before you programme.

So in this lesson, you'll plan an algorithm for drawing your patterns.

You can choose to design your algorithms either as an annotated drawing or as a sketch and list.

And we'll have a look at some examples of those in a couple of minutes.

One way of planning an algorithm is to create an annotated drawing.

This is where you draw shapes and label them with angles, the directions they will turn, and the number of repeats.

So here's an annotated drawing.

So we've got a drawing of what we want the kind of output to look like.

So some shapes repeated with turns in between.

So we want a square, repeated nine times, with a turn of 40 degrees each time, a right turn of 40 degrees.

And this is kind of what the code might look like with the procedure square.

So we could say to square, repeat 4, square brackets, forward 100, right turn 90, and then end.

So that's our procedure.

And then we can run that procedure nine times to create the drawing.

You can also create a sketch and an algorithm.

You write the steps in order, including repeat loops.

You also draw a simple sketch.

You could indent the commands to be repeated, making your algorithm a bit easier to read.

So here's an example.

So again, we've got the sketch of the drawing, which is similar to our annotated drawing, but this time we've got what the code might look like.

So draw a square, and then indented, repeat four times, draw a side 150, turn 90 degrees.

And then we have repeat nine times, draw a square, turn 40.

So there's our procedure at the top and there's what the code might look like at the bottom, including the procedure.

So this isn't the actual code, but it kind of looks a bit like it, and it will really help you to write your own code when it's the time for you to do so.

So either on your own, or with a partner, if you have one, reflect on the two different design approaches.

So, A, the annotated drawing, or B, a sketch and algorithm as a list.

Now it's time for you to decide which one you will use for your wrapping paper plan, and then plan your programme.

So use the design sheet to plan your programme.

And your plan will end up looking something like this.

So now that you've designed a programme that uses count-controlled loops, you can implement that design as a programme.

Now that you have a plan to create a repeating shape pattern, you can use it as the starting point for your wrapping paper programme.

Procedures can be used to create shapes of different sizes.

Here, we have examples of procedures to create squares of two sizes.

So we have big square on the left and small square on the right.

The first shape has longer sides, so the square will be bigger.

The second shape will have shorter sides.

So here, we can see the difference.

So the big square has sides of 150 and the small square has sides of 50.

Procedure names cannot have any spaces.

So let's have a look at this animation demonstrating the procedures for squares of different sizes.

So first, we're going to create to bigsquare, repeat 4, square brackets, forward 150, right turn 90.

And then if we type bigsquare in the programme, it will create a big square.

So now we're gonna create the procedure to smallsquare.

So this time, again, repeat 4, forward, just 50 this time, still with the right turn of 90.

And then if we type smallsquare, you'll see there inside the big square, we have a small square.

New command there, so cs clears the screen.

We'll talk about that in a minute.

So bigsquare, smallsquare.

Think about how you want to move your turtle in between the repeating shape patterns.

So here's the commands that you can use.

There's some that you've already seen and some new here.

So we have fd to move the turtle forward, bk to move the turtle backwards, rt for a right turn, lt for a left turn, cs, which you've just seen, will clear the screen.

pu will lift the pen up, pd will put the pen down, and home, the turtle returns to the start, pointing up, but any lines on the screen remain.

So let's just go through those again.

fd for forward, bk for backward, rt for right turn, lt for left turn, cs for clear screen, pu for pen up, pd for pen down, and home to return the turtle to the start, pointing up, with the lines remaining.

If you find any mistakes in your programme, you will need to debug any errors.

Debugging is finding and fixing those errors.

If you find any mistakes in your plan, you should correct them with a pen of another colour.

This is so that you can track any changes you make.

So we've got a couple of bugs in some of the programmes here.

So Aisha says, "Help! My turtle leaves a line when it moves between shapes and I don't want it to." Alex says, "Use pen up to stop the turtle drawing, and then pen down when it's ready to start drawing again." So, pen up will lift the pen up off the screen or the page, so it won't draw anymore.

And pen down will put the pen back down, so it will draw.

Jacob says, "Help! I'm using a lot of code to move the turtle back to the same place." Lucas says, "Use the home command to move the turtle back to the starting position." So, home returns the turtle back to the start, pointing up.

And Lucas says, "Help! My turtle doesn't create the pattern that I'd planned out." Aisha says, "Look closely at the turn angles and repeats in your shape pattern plan and code." So, look closely at the turn angle.

So here's the angle, right turn 90, and the root number of repeats.

So there's four repeats in this code.

So this is what Lucas could look at, the turn angles and the repeats in the shape pattern plan and code.

Now, see if you can fill in the blanks here.

The Logo command that stops the turtle drawing is called.

The Logo command that resets the turtle to the starting position without removing drawn lines is called.

If your turtle isn't drawing the shapes that you planned, look closely at the.

And repeats in your shape pattern plan and code.

So let's have a look at the answers for these.

So the Logo command that stops the turtle drawing is called pen up, pu.

The Logo command that resets a turtle to the starting position without removing drawn lines is called home.

And if your turtle isn't drawing the shapes that you planned, look closely at the turn angles and repeats in your shape pattern plan and code.

Once you have your pattern repeating correctly, it's time to add colours.

So here's a new command, setpc, which represents set pen colour.

Set the pen colour and then a number will change the colour of the line.

So if you choose setpc 4, it will set the pen colour to red.

So here's an animation demonstrating how to change the colour of the pen line.

So setpc 1, forward 100, and we have a blue line.

setpc 4, forward 100, and we have a red line.

There's lots of different colours you can choose to make your pattern stand out.

So these numbers will change your pen line to the colour by the number.

So 0 will create a black line, 1 is blue, 2 is green, 3 is cyan, 4 is red, 5 is magenta, 6 is yellow, 7 is white, 8 is brown, 9 is tan, 10 is dark green, 11 is aqua, 12 is salmon, 13 is purple, 14 is orange, and 15 is grey.

And Lucas is asking, "How could you use colours in your pattern?" So time for another true or false question.

So setcp is the command to change the pen colour.

Is that true or false? Well done.

That's false.

But can you tell me why? That's right.

The command is setpc, which is short for set pen colour.

So now for your next task, use your wrapping paper design to create your programme in Logo.

Once your shape code is repeating correctly, use setpc to add coloured lines to your pattern.

So here's an example of the type of code you could use.

So we've got setpc 13, setting the pen colour to begin with.

Then we have repeat 8, and in square brackets, bigsquare, smallsquare, right turn 45.

Then we have pen up, left turn 90, forward 200, right turn 90, pen down, setpc 14.

And then repeat 8, bigsquare, smallsquare, right turn 45.

Pen up, left turn 90, forward 200, right turn 90, pen down, setpc 1.

Repeat 8, bigsquare, smallsquare, right turn 45.

Pen up, left turn 90, forward 200, right turn 90, pen down, setpc 10.

And then finally, repeat 8, bigsquare, smallsquare, right turn 45.

And that creates the pattern you can see on the right of the screen.

So even though there's lots of different, looks like there's lots of different shapes in here, these are all in fact drawn by squares with different repeats, different colours, and different turns in between those repeats.

So this is all made up of squares, no other shapes.

Let's look at a summary of this lesson.

Today, you've created a procedure, which is a named block of code that can be used multiple times in a programme.

You've made a count-controlled loop, which repeats a set of commands a specific number of times, making it useful for creating patterns.

And you can now make a plan to decide which shapes, colours, and repeats to use in a programme.

You can then turn this plan into a programme using procedures and loops in Logo.

You can test and debug your programme to ensure that your final pattern matches the original plan.

Well done for completing today's lesson.

I look forward to learning with you again soon.