5 Repeating Code

Learning Outcomes

  • Understand the concept of efficiency in programming and recognize scenarios where using loops can optimize your code
  • Apply the “repeat block” to repeat code a specified number of times, enhancing your ability to perform repetitive tasks in programming.

Shapes with Loops

imageUsually, certain shapes have a set number of degrees that the internal angles add up to. For example, a square’s angles add up to 360°, while a triangle’s angles add up to 180°. For shapes that have sides of equal length, you will learn how to use a loop to write more efficient code.


Square code

To appreciate the value of a for loop, first think about the code for driving in a square. Since a square has four equal sides, you would need to go forward then turn left or right four times.

image

Use the driving blocks to write a code that drives Zumi in a square. You have done this before so this should be a review!

zumi blockly square code


Loops

You have probably noticed that you repeat the same section of code four times. That’s a lot of blocks! To get around this, you can use a loop, which lets you repeat parts of your code. They’re a great shortcut — without them, you would always have to use the same blocks over and over again. They are called loops because the last statement loops around back to the beginning. The loop blocks that help you repeat code are found in the “Loops” menu.

zumi blockly loops menu


Repeat block

Any code attached to a repeat block will repeat as many times as you set. This means you can choose how many times your code will repeat! Here is the same square code written with a repeat block.

zumi blockly square code with repeat


More shapes!

What other shapes can you make with loops? The square is the easiest because each turn is 90 degrees, but what about other shapes? How many sides does a triangle have? This particular triangle will be an equilateral triangle, meaning all three sides are of equal length and every angle is the same number of degrees. Here’s a hint: the exterior angles of all regular polygons, or shapes with equal sides, always add up to 360 degrees.

image

There are many other shapes you can teach Zumi. Try pentagons, hexagons, or octagons! You may need a pencil and paper to figure out how many degrees you need to turn. Remember, the number of degrees multiplied by the number of sides should be equal to 360 degrees.

image


More Shapes – Solutions

Once you have tried making the different shapes with Zumi, try comparing your code with the solutions below!

Pentagon:

image

Hexagon:

image

Octagon:

image


Review

In this lesson, you learned how to use the repeat block! The repeat block is helpful when you want to repeat code a certain number of times. In the next lesson, you will learn about conditionals and how we can make loops run as long as something is true.


Test Your Knowledge


Review The Key Concepts


Solve The Problem

Scenario: You are trying to make your code more efficient. Previously you have created a program for an autonomous vehicle to make 4 left turns, to create a square.

Problem: How can you efficiently program your Zumi to drive along a hexagonal shape? What other shapes can you create using efficient code?


Sample Solution

There are many different ways that we can solve the problem listed above. Please watch the following video to review a sample solution completed by Ontario Tech University Engineering Students.

Demo

Code


Common Errors

Errors can also occur while creating a solution to the problem. Please review the video below to view some of the common errors that can occur.

Common Errors Video: Coming Soon.


Connecting The Dots

License

Foundations of programming with Blockly and Zumi Copyright © by Brandon Carson. All Rights Reserved.

Share This Book