Page 191 - Learn To Program With Scratch
P. 191

The outer loop tries all values of n1 from 1 to 23. For each value of n1,
                          the inner loop tries all values of n2 from 1 to (24 – n1). For each combination
                          of n1 and n2, the script sets n3 equal to 25 – (n1 + n2), and then it checks to
                          see whether the sum of the squares of these three numbers is 243. If it is,
                          the script says the answer and stops.



                                                    try it out 7-5
                            Create the script shown in Figure 7-16 and run it to find n , n , and n  . If you study
                                                                               3
                                                                         2
                                                                       1
                            the script carefully, you’ll find that it tries some (n , n ) combinations more than
                                                                   2
                                                                1
                            once . For example, the numbers (1, 2) are tested in the first iteration of the outer
                            loop, whereas the numbers (2, 1) are tried in the second iteration . These two tests
                            are redundant; we only need one of them . You can fix this by having the inner
                            loop start from n  instead of 1 . Make this change to the script and then run it to
                                         1
                            make sure it still works as expected .

               recursion: Procedures that call themselves

             Recursion .sb2  The repetition structures introduced so far allow us to repeat a command
                          or a set of commands through iteration. Another powerful technique that
                          produces repetition is recursion. Recursion allows a procedure to either call
                          itself directly or do so indirectly through another procedure (for example,
                          A calls B, B calls C, then C calls A). It may not be obvious why you want to do
                          this, but it turns out that recursion can simplify the solution of many com-
                          puter science problems. Let’s demonstrate this concept by considering the
                          simple example shown in Figure 7-17.

                                                 Call Tic again.
                                                 This should continue
                                                 forever!







                          Figure 7-17: A recursive procedure

                             The Tic procedure executes two say commands (the first says “Tic” and
                          the second says “Tac”), then calls itself again. The second call does the same
                          thing, and the sprite would continue saying, “Tic Tac” forever if no outside
                          action stopped it. Of course, the only way to stop it in this case is to click the
                          red stop icon. Having a procedure call itself this way allowed us to repeat
                          the two say commands forever without using any loop blocks. The form of
                          recursion used in this example is called tail recursion because the recursive
                          call is located at the very end of the procedure. Scratch also allows recursive
                          calls to come before the last line, but we won’t explore that type of recur-
                          sion in this book.

                                                                  Repetition: A Deeper Exploration of Loops   169

                                           www.it-ebooks.info
   186   187   188   189   190   191   192   193   194   195   196