Counting the number of comparison expressions, there are nine expressions that must be evaluated every time the program runs. As written, the program must check all five statements, even if the letter grade was assigned by the first decision. Suppose the percentage is 82. The second decision solves the problem, but all statements must be checked. However, writing the above logic statements as nested decisions will streamline the code. Nested decisions are processed only until the problem is solved. Once the solution is found, no more of the nested conditions are tested. This is much more efficient code. No more conditions are tested than is necessary. Consider these nested logical statements. percent = ROUND (percent) IF percent 89 THEN letter = A ELSE IF percent 79 THEN letter = B ELSE IF percent 69 THEN letter = C ELSE IF percent 59 THEN letter = D ELSE letter = F The Scratch code for these nested decisions is shown in Figure 5-8. Suppose the average is 78.2. The first test is percent 89, which is false, and control passes to the next ELSE statement. The test percent 79 is also false, so the control passes to the next ELSE clause. The test percent 69 is true, so control passes to the next THEN clause, letter is set to C, and execution stops. The remaining test is skipped. Nesting IF…THEN and IF…THEN…ELSE statements allows for complex responses. You can use many nested statements. There is no limit to how many statements can be nested. However, the more nesting, the more difficult it is for the programmer to keep track of the code’s logic. To create nested statements in Scratch, add the if then or if then else block within another if then or if then else block, as shown in Figure 5-9. Indentation is used in code to help the programmers see how the pieces of code fit together. It is only for the programmer’s benefit and has no effect on the processing of the code. In de n tat i o n i s used in code to hel p the programmers see how the p ieces of code fit together. It is onl y for the pr ogrammer ’ s benefit and h as n o effect o n t h e processing of the code . FYI FYI set to percent percent round else if then percent 89 set to letter A else if then percent 79 set to letter B else if then percent 69 set to letter C else if then percent 59 set to letter D set to letter F Goodheart-Willcox Publisher Figure 5-8. This code is nested decisions for determining a letter grade based on a percentage score. if else then if then GoRight = yes speed = run change x by 100 change x by 10 Nested statement Main statement Goodheart-Willcox Publisher Figure 5-9. A nested block of code is contained within another block of code. Copyright Goodheart-Willcox Co., Inc. 112 Introduction to Computer Science: Coding