Chapter 1 – Introduction to Blockly with Zumis
1.5- Repetition Code Structure
This module will teach you about using loops in Zumi’s Blockly programming language. Loops are used to repeat parts of your code, making it more efficient and concise. This tutorial will walk you through using a loop to write code for driving Zumi in a square, and then show you how to use loops to create other shapes like triangles, pentagons, hexagons, and octagons.
## Step 1: Shapes with Loops
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 can use loops to write more efficient code.
## Step 2: 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.
Use the driving blocks to write code that drives Zumi in a square. This should be review!
## Step 3: Loops
To avoid repeating the same section of code four times, you can use a loop, which lets you repeat parts of your code. Loop blocks are found in the “Loops” menu.
## Step 4: Repeat block
Any code attached to a repeat block will repeat as many times as you set. Here is the same square code written with a repeat block.
## Step 5: More shapes!
Using loops, you can create other shapes like triangles, pentagons, hexagons, and octagons! Remember, the number of degrees multiplied by the number of sides should be equal to 360 degrees.
### Problem
Try to write a loop structure to make Zumi drive in a pentagon, hexagon, and octagon before moving to the solution!
### Solution
#### Pentagon
The following is the solution to make Zumi drive in a pentagon using a loop structure:
#### Hexagon
The following is the solution to make Zumi drive in a hexagon using a loop structure:
#### Octagon
The following is the solution to make Zumi drive in a octagon using a loop structure:
## Step 6: Review
In this lesson, you learned how to use the repeat block to make your code more efficient. In the next lesson, you will learn about conditionals and how they can be used to make loops run as long as something is true.
## Demo
## Questions