Executing Loops

Most computer applications and games are interactive, meaning that they respond to user input and react accordingly. In doing so, it is often necessary to execute collections of code statements repeatedly. For example, an arcade-style computer game might require the continuous playback of background music and sound effects. This would require the repeated execution of programming logic required to manage sound playback for as long as the game was played. To manage this type of interaction, you need to add loops to your applications. In Scratch, a loop is a collection of one or more code blocks embedded with a control block that are repeatedly executed.

Without loops, programmers would have to create extremely large scripts filled with repeated series of duplicate statements to perform certain tasks. For example, to create a Scratch application that bounces the Cat sprite up and down four times without a loop, would you have to add a script like the one shown next to the sprite.

The script begins by positioning the sprite at the bottom center of the stage. Two sets of motion blocks are needed to bounce the sprite one time. So to bounce the sprite up and down four times, these two code block have to be repeated four times. Suppose you wanted to make the sprite bounce 10, 100, or 1,000 times. Clearly, this is a situation where a loop is needed.

Scratch supplies access to two code blocks that you can use to set up loops, as shown in Figure 9.4.

Figure 9.4. Using these code blocks, you can create loops that repeat the execution of any code blocks you embedded within them.


Note

Scratch also supplies two additional control blocks that offer the capability to conditionally execute loops. These two code blocks will be discussed a little later in this chapter when conditional logic is covered.


The first of the two code blocks shown in Figure 9.4 can be set up as a loop that executes forever, which really means that the loop repeatedly executes until the script in which it resides is halted. For example, the following script uses this code block to set up a loop that bounces a sprite over and over again, until the Stop Everything button is clicked:

The first statement moves the sprite to the bottom center of the stage. The two statements within the loop bounce the sprite, in a gliding motion, up and down from the bottom to the middle of the stage.

Note

In Scratch, there are two ways to force an immediate termination of a script. First, you can halt a script by stopping the execution of the application by clicking on the red Stop Everything button. However, this option can often be a bit of overkill. As a less extreme option, Scratch offers a control block that allows you to halt an individual script’s execution. There is also a control block that you can use to halt the execution of all scripts within an application. Both of these control blocks are reviewed a little later in this chapter.


Rather than repeating the execution of a loop forever, you can use the second code block shown in Figure 9.4 to set up a loop that executes a predetermined number of times. For example, the following script demonstrates how to bounce a sprite up and down a total of 10 times.

Obviously, the fewer code blocks you use when developing scripts, the more streamlined and easier to support your applications will be. Loops make programming a lot easier and provide a tool that you can use to repeat the execution of any number of code statements with as little fuss as possible.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.23.127.197