Patterns and Loops
Any time you find yourself using the same blocks of code repeatedly, you have just identified a pattern! Let me repeat that: if you find yourself using the same blocks of code repeatedly, you have identified a pattern.
A great way of handling patterns with code can be handled with "loops." Loops allow you to repeat blocks of code. In Scratch
Links to an external site., loop blocks can be found by clicking on the "Control" circle in the "Code" tab.
There are multiple types of loops available in Scratch. The three main loops in Scratch are:
- repeat -->
. Repeats the code inside of the loop the # of times indicated by the # at the top of the loop (10 times in this image).
- forever -->
. Repeats the code inside of the loop forever.
- repeat until -->
. Repeats the code inside the loop until some condition is met (in this case, until the sprite is touching the red color).
Looping Challenges
Click this link to open the Scratch interface in another window. Links to an external site. Then, try to accomplish the following 3 tasks by identifying the pattern you need and using the appropriate loop to accomplish that task.
-
Make Scratch Cat glide around the edge of the screen using just the glide block and turn block.
-
Make Scratch Cat walk back and forth across the screen continually. HINT: use the following two movement blocks to help with the illusion that Scratch Cat is pacing:
- Make Scratch Cat walk across the screen until it hits a blue wall. (HINT: You'll need to add a blue wall to test this)