In a previous lesson, you learned how to draw a square, where you dragged and dropped a set of blocks a specified number of times. Now you will learn an easier way to code a program that draws a square, using a Repeat block and a Repeat Until block.
The Repeat block repeats a set of blocks a specific number of times. For example, your square program uses a set of instructions four times.
The program begins with the following blocks:
• When green flag clicked
• Pen down
These blocks are not in the Repeat block, since they only need to run when the program starts.
To use a Repeat block, perform the following steps:
1. Click Control.
2. Drag and drop the Repeat 10 block to the script area. Change the 10 to 4.
3. Drag and drop the Move and Turn blocks. Place them inside the Repeat block. Remember to change the degrees in the Turn block to 90 and the steps in the Move block to 100.
Here is the completed program.
Click the green flag. The program draws a square just like the original program.
Repeat Until Block
The Repeat Until block repeats a set of blocks until a particular value is received or a specific event occurs. You’ll want to add some blocks to your square program that asks the user if they want to draw another square. If the user answers “Y” for yes, the program draws another square. If the user enters “N” for no, the program ends.
The Repeat Until block checks the value of the variable Answer and based on
the value, either draws a square or the program stops.
Once the program draws a square, the Pen Up block executes because the program moves the sprite a specific number of steps. The reason for this is to prevent the sprite from drawing a line when it moves to its new position. The program moves the sprite, so the sprite does not draw the new square over the previously drawn square.
The Repeat Until block checks the value of the answer variable. If it is “N,” the program ends. If it is “Y,” the program draws a new square.
So, now you know the easy way is to repeat a set of blocks as needed. The next lesson shows you how programs make decisions.
Share with friends