Page 178 - Learn To Program With Scratch
P. 178

are also called counter-controlled loops or counted loops. Other types of loops
                         continue to repeat until some condition occurs; these are called condition-
                         controlled loops or indefinite loops. Another loop, called an infinite loop, repeats
                         forever.
                             In this chapter, you’ll learn about the different repetition structures
                         available in Scratch. I’ll explain both counter-controlled and condition-
                         controlled loops in detail, and I’ll introduce you to the stop block, which
                         you can use to end infinite loops. You’ll learn how to use loops to validate
                         user input, as well.
                             The chapter also discusses nested loops (loops that contain other loops)
                         and shows several examples of their use. We’ll also talk about recursion—a
                         procedure calling itself—as another way to achieve repetition. At last, we’ll
                         develop a number of interesting applications that use both loops and condi-
                         tionals, and we’ll look at incorporating loops into practical programs.


              more Loop blocks in Scratch

                         As you learned in Chapter 2, loop blocks allow you to repeat a command
                         or a set of commands in a program. Scratch supports the three repetition
                         blocks shown in Figure 7-1.





                         Figure 7-1: Scratch’s repeat blocks


                             You’ve already used two of these blocks, the repeat and the forever
                         blocks, in many examples throughout this book. In this section, we’ll exam-
                         ine the third kind of loop block—the repeat until block—and explain
                         some of the technical terms associated with loops in general.
                             Each repetition of a loop is called an iteration, and the word count
                         is often used to describe the number of times a loop repeats. The repeat
                         block, which you are very familiar with, is a counter-controlled loop because
                         it repeats its commands a specific number of times. We usually use this loop
                         when we know the number of repetitions the loop will need to execute,
                         as when we want to draw a polygon with a known number of sides.
                             On the other hand, the repeat until block is a condition-controlled loop.
                         The statements inside this block are repeated based on the truth or falsity
                         of its test expression. We use this block when we don’t know in advance how
                         many times the loop will need to repeat and want the repetition to continue
                         until some condition is met. You can say, for example, “Repeat the ask com-
                         mand until the user enters a positive number.” Or, “Repeat firing missiles
                         until the player’s energy level drops below a certain value.” The following
                         sections explain condition-controlled loops in more detail.







           156   Chapter 7
                                           www.it-ebooks.info
   173   174   175   176   177   178   179   180   181   182   183