« Back to Main Page

Intermediate Loops

From Bold Idea Knowledgebase

Introduction

A loop is the action of doing something over and over again. In coding, 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.

There are many types of loops. In the following activities, you’ll learn how to create For Loops with a set of parameters and Nested Loops.

What is it?

A loop is the action of doing something over and over again.

A ‘for loop’ has a pre-determined beginning, end and increment.

Nested loops are looped statements inside another loop.

What is the difference between a loop and a function?

Both functions and loops allow you to group together code and execute it over and over again. Functions, however, allow you to give that grouping of code a name, and then use that name anywhere in your code, any number of times.

In the Artist Functions puzzle, you’re tasked with drawing some square glasses. This means that you have to draw two squares with a bar in between. We already know how to draw a square, and we know we don’t want to have to write that same code all over again. If we used a loop, we would only be able to draw two squares, or maybe two squares and two bars. With a function, however, we can draw a square once, then draw a bar, then one more square.

LoopVsFunction.png

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. The puzzle is a great way to practice converting sets of actions into a single For 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 Nested Loops

With a little bit of geometry, you can write programs that have looped statements inside another loop in the Artist: Nested Loops puzzle on Code.org. This is called a Nested Loop. Refer to the Geometry Guide below for help on the angles. For each shape, consider how you might explain to someone how to draw that shape. How could you draw the shape using a loop?

ArtistNestedLoops.png

Repeated Patterns

In Pencil Code, drawings with repeated patterns – or loops – can be created using the for block. Locate the ‘Repeated Patterns’ project in Pencil Code and practice adjusting the number of times the code block is repeated. How does the shape change if it’s repeated six times – or just once?

Complex Repetitions

You can create intricate effects in Pencil Code by nesting for loops inside other for loops. Locate the ‘Complex Repetitions’ project in Pencil Code and practice adding more colors or numbers to the outer and inner loops. How many shapes are drawn when you change the repetitions? Can you change the arrangement of the shapes?

Design Your Own Shape

Get creative and design a shape that’s all your own in Pencil Code’s ‘Make Your Own’ space using for loops. Feel free to copy code or get inspiration from other projects. Then, share your design with others on your team.