Experiment with Repeat
In Game of Tag, you experimented briefly with the
forever block. This “infinite loop” construct is often useful, but a more common scenario is repeating an action for a specific number of times (i.e., when the number is less than ∞!). Scratch makes this a simple task with
Repeat blocks. In the Unit 1 activity, Flow Patterns, you explored the basic components that can constructs any algorithm: sequence, selection, and iteration. Repeat is an example of the iteration component of algorithmic design. We can use a
Repeat to make drawing shapes a lot easier! You can see below a script to draw a square.
Computer scientists often call this type of “control flow” a loop, because the flow cycles back around to a point at which the program has already executed an instruction. In the case of an infinite loop (
forever), the cycling continues as long as the program remains executing. In the case of a
repeat block, the loop only executes the number of times specified by the parameter (in the case of the square, four times). Use your finger to trace the control flow through the program. How does the word “loop” apply to your movements?
repeat, command, draw the following regular* shapes:
- Equilateral Triangle
- Five-sided Star
What patterns can you identify between these shapes and the commands?
*“Regular” means all the sides are the same length.