Page 188 - Learn To Program With Scratch
P. 188

sum          count
                                                          0            2
                                                           +
                                                          2            4
                                                           +
                                                          6            6
                                                           +
                                                         12            8
                                                           +
                                                         20           10
                                                         ...          ...
                         Figure 7-14: This script finds the sum of all even integers from 2 to 20 .

                             This script starts by initializing the sum variable to 0 and the count
                         variable to 2, and then enters a conditional loop that repeats until count
                         exceeds 20. Each time the loop iterates, the value of count is added to the
                         current sum and the count variable is increased by 2 to get the next even
                         integer in the sequence. Predict the output of this script then run it to
                         check your answer.



                                           non-integer rePeat count

              Non-Integer    What do you think would happen if you asked Scratch to repeat a loop
           RepeatCount .sb2  2 .5 times? The three examples shown below demonstrate how Scratch
                               handles non-integer repeat counts .

















                                Of course, there is no such thing as “repeat 2 .5 times,” but Scratch doesn’t
                             prevent you from entering such values . Rather than giving an error message,
                             Scratch automatically rounds a decimal repeat count to its nearest integer .












           166   Chapter 7
                                           www.it-ebooks.info
   183   184   185   186   187   188   189   190   191   192   193