Beginner Loops
Contents
Introduction
Computers are really good at repeating actions. You can count to ten, twenty or one hundred, but a computer can count to a billion or a trillion. It won’t get bored, and it will only take a few seconds. Whether it’s counting, drawing or anything – computers can repeat things hundred and even millions of times.
In coding, we call this a loop. A loop is how you repeat your code over and over again. When you know in advance that you want to do something a certain number of times, it’s easier if you tell the computer to “repeat it that many times” using a loop.
What is it?
A loop is the action of doing something over and over again.
Activities
Maze Loops
Work through the Maze: Loops puzzle on Code.org and see if you can figure out how many fewer blocks you can use with the loop vs. not using a loop.
TIP: If you get stuck building the loop, try solving the puzzle without using any loops at all. Then, look at the code and try to see where things are repeated. Break apart the repeated blocks into groups and see if each group looks exactly the same. If they do, you can throw away the other blocks and use a loop.
Artist Loops
With a little bit of geometry, you can draw more complex images by looping simple sequences of instructions in the Artist: Loops puzzle on Code.org. For each shape, consider how you might explain how to draw that shape. How could you draw the shape using a loop?
Snowflakes
You may remember in science class that when water crystallizes it forms repeating patterns in snowflakes and on frosty surfaces. Look at several pictures of snowflakes. What shapes do you see? Do any of them repeat? Can you see the pattern?
In this craft activity, you’ll make a paper snowflake by designing a pattern and deciding how many times to repeat that pattern. Use small square sheets of paper, fold the papers in half and then cut a simple design. How many times is the design repeated? What are the points of symmetry? Tutorials on Instructables.com provide step-by-step instructions for repeating or looping your pattern up to six times.
Try it with code: In the Code with Ana and Elsa game on Code.org, help the Frozen characters create beautiful snowflakes using the “repeat” block. Any blocks you put inside the “repeat” block will be repeated in sequence, as many times as you want.
Moving Animation in Scratch
Give a sprite movement in Scratch by creating an animation. Pick a sprite that has more than one costume, like a parrot in the example below. Then, write a new program that will animate the sprite using a repeat block that changes its costume.
Dance Party
Have you ever been to a dance party? In Scratch, you can build your own dance party by combining animation and music. Work together in pairs to follow the ‘Getting Started with Scratch’ tutorial, and be sure to follow each step as you’re building your project. Be creative as you want: Maybe your dance party is on the beach or even on a soccer field. When you’re done, share your dance party creations with the other members of your team. What did you like about their projects?
This script in Scratch conserves space, which is quite useful if there are many costumes being displayed.