Page 189 - Learn To Program With Scratch
P. 189

revisiting nested Loops
                          Back in “Rotated Squares” on page 34, we used nested loops to draw
                          rotated squares. One loop (the inner loop) was responsible for drawing the
                          square, while the other loop (the outer loop) controlled the number of rota-
                          tions. In this section, you’ll learn how to use the concept of loop counters in
                          conjunction with nested loops to create iterations in two (or more) dimen-
                          sions. This technique is an essential part of programming and, as you’ll see
                          in a moment, can be used to solve a wide range of programming problems.
                             To set the stage, let’s say that a local restaurant offers four kinds of pizza
                          (P1, P2, P3, and P4) and three kinds of salads (S1, S2, and S3). If you ate
                          there, you would have 12 possible combinations to choose from; you could
                          have P1 with any of three salad types, P2 with any of three salad types, and
                          so on. The restaurant’s owner wants to print out a menu that lists the avail-
                          able pizza/salad combinations along with their combined prices and calorie
                          contents. Let’s see how nested loops can be used to generate a list of all pos-
                          sible combinations. (I’ll leave calculating the prices and calorie content as
                          an exercise for you.)
                             If you think about it, you’ll see that we just need two loops: one loop
                          (the outer loop) to cycle through the pizza types and another loop (the
                          inner loop) to cycle through the salad types. The outer loop starts with P1,
                          while the inner loop tries S1, S2, and S3. The outer loop then moves to P2,
                          and the inner loop again chooses S1, S2, and S3. This continues until the
                          outer loop has passed through all four pizza types. An implementation of
                          this idea is illustrated in Figure 7-15.

             NestedLoops1
                     .sb2                                                  P=1
                                                                         3  S  1
                                                                      1     2     3
                                                                   P=4  S  2   2  S   P=2
                                                                      3     2     1
                                                                         1  S  3
                                                                           P=3

                          Figure 7-15: Visualizing nested loops . The variable P controls the outer loop and
                          the  variable S controls the inner loop .

                             The script uses two loops and two counters. The counter for the outer
                          loop is named P, and the counter for the inner loop is named S. In the first
                          iteration of the outer loop (where P = 1), the value of counter S is set to 1,
                          and the inner loop repeats three times. Each time, it executes a say com-
                          mand to display the current values of P and S, and then it increments S by 1.
                          Thus, the first iteration of the outer loop causes the sprite to say “P1,S1” and
                          “P1,S2” and “P1,S3.”




                                                                  Repetition: A Deeper Exploration of Loops   167

                                           www.it-ebooks.info
   184   185   186   187   188   189   190   191   192   193   194