Quantcast
Channel: Adventures in Scratch by Andrew Murphy - Blog
Viewing all articles
Browse latest Browse all 30

Loops in Scratch

$
0
0
Picture
Looping (or iteration) is a very important concept in programming.
It is usual by this stage in our Scratch adventures that many of the children's scripts have become quite long. Often bits are repeated. The idea that we can tell the computer to repeat a set of instructions as many times as we like is very powerful, but I find children first appreciate it because it means less work!

We could at this point extend our pollen gathering bee project, by asking what the bee would do the next day and then examining how we could get the bee to repeat its path using a loop. However I find that it pays to vary the topic or theme of these instructional parts of learning Scratch. It keeps the students interested. It demonstrates to them the vast range of potential projects in Scratch. In fact using loops allows the children to begin to incorporate their own interests into their Scratch projects. This is a very powerful motivator.
Picture
As a result, I usually introduce loops by demonstrating simple sprite animation. Some of the animals, including Scratch, the parrot, bat, dog and others have costumes suitable for animation. After importing the needed costumes (ask the children how this is different to getting a new sprite), I begin by demonstrating the switch_to_costume block, asking the students how they would make it look as though the wings are flapping.

Picture
We'll often end up with something like this. In fact I try to end up with something like this. It makes sense. It shows that they've got the idea of sequencing.
It doesn't work but that's okay. We'll try again. We'll persevere.
So why doesn't it work? If no one volunteers an answer (someone usually does) I ask them how long they think this script would take to run through. how long does it take for the computer to change a costume. Try it out if they can't say.
It doesn't take long for someone to suggest that the computer needs to wait a bit before changing costumes.

Picture
That's better - the wings are flapping. A bit slow maybe, but I'm sure the children will figure out how to speed it up a bit. Even if the first time they try, they slow it down even more!
It's okay to make mistakes. It's okay to try something out and then go back and make it work or work better. Children love doing this. And I find it's how they learn best.

And then it's time to introduce the power of looping. Does anyone notice anything about this script? Someone will get it. Make sure you wait long enough. Then ask him or her to separate the script up into the bits that are repeated (interactive whiteboards are great for this).
I then model how to wrap a forever loop around the bit that was repeated. I ask them what they think it will do. And sure enough it does what they think.
Then I'll show them a repeat block. I ask them what this will do and what will happen if I change the number.
The it's their turn. This is a great lesson to allow the children to use their interests from the start. There are quite a few sprites which have enough costumes for animating - from a flapping bat to a snapping shark. Some might want to draw their own. Or perhaps add a costume to the bee. The latter invariably leads to a new question:
"Can I make it move and flap its wings at the same time?"


I tell them to give it a try.
After all, if it goes wrong the first time they can try again...

Viewing all articles
Browse latest Browse all 30

Trending Articles