Page 154 - Learn To Program With Scratch
P. 154

Figure 6-7 shows two sample outputs for when the user enters 6 and 9,
                         respectively, in response to the ask command. Can you explain how this
                         script works?

                         Nested if and if/else Blocks

                         If you want to test more than one condition before taking an action, you
                         can nest multiple if (or if/else) blocks inside each other to perform the
                         required test. Consider for example the script shown in Figure 6-8, which
                         determines whether a student should receive a scholarship. To qualify, the
                         student must have: (1) a grade point average (GPA) higher than 3.8 and (2)
                         a grade above 92 percent in math.





                                                          No  gpa  Yes     mathScore
                                                             > 3.8?         > 92?
                                                                          No     Yes
                                                        say
                                                     “Sorry! Low        say         say
                                                       GPA.”         “Sorry! Low  “Congratulations!”
                                                                     math score.”




                         Figure 6-8: You can use nested if/else blocks to test multiple conditions .

                             First, the expression gpa > 3.8 is tested. If this expression is false, we
                         don’t need to check the other condition because the student doesn’t meet
                         the scholarship criteria. If the expression gpa > 3.8 is true, however, we
                         need to test the second condition. This is done with the nested if/else block,
                         which tests the condition mathScore > 92. If this second condition is also
                         true, the student gets the scholarship. Otherwise, the student does not qual-
                         ify, and an appropriate message explaining the reason is displayed.

                         Menu-Driven Programs

            AreaCalculator   Next, we’ll explore a typical use of nested if blocks. In particular, you’ll
                     .sb2  learn how to write programs that present the user with choices and act on
                         the user’s selection.
                             When you start up some programs, they display a list (or menu) of avail-
                         able options and wait for you to make a selection. Sometimes, you’ll interact
                         with these programs by entering a number that corresponds to your desired
                         option. Such programs may use a sequence of nested if/else blocks to deter-
                         mine the user’s selection and act appropriately. To see how nested if/else
                         blocks work, we’ll discuss an application, shown in Figure 6-9, that calcu-
                         lates the area of different geometric shapes.





           132   Chapter 6
                                           www.it-ebooks.info
   149   150   151   152   153   154   155   156   157   158   159