Section 5.3 Coding Decisions The basic building blocks for Boolean expressions in Scratch are based on the IF…THEN and IF…THEN…ELSE statements. These statements are represented by blocks of the same names found in the Control category. These blocks are used to make decisions and control actions in programs. IF…THEN Block The if on edge, bounce block used earlier is a shortcut of the basic IF…THEN statement. The beginning of the standard IF…THEN statement contains the condition to test, as shown in Figure 5-5. If the condition is true, Scratch will execute the code contained below within the block. If the condition is false, Scratch will proceed to the next line of code after the IF…THEN statement. Conditional operators can be included to test multiple conditions. To provide multiple outcomes, add the code for each outcome in the space within the block. Scratch will process the lines of code sequentially before leaving the if then block. The IF…THEN statement is the most basic logic statement in computer programming. No matter how it is coded in a language, it is the basis for all decisions made by the resulting program. The IF…THEN statement is the most basic lo gic statement in computer programming. No matter how it is coded in a specificspecific lan g ua g e, it is the basis for all decisions made by the resultin g pro g ram . FYI FYI if then Condition to test is inserted here Code for outcome is inserted here Goodheart-Willcox Publisher Figure 5-5. The if then block in Scratch is how an IF…THEN statement is coded. Hands-On Example 5.3A Hands-On Example 5.3A Basic IF…THEN H Construction To be able to vot te in the United States, a person m mu st be 18 years of age. The algorithm is: determine if the number m entered is greater than or eq qu al to 18. Use numbers, operators, user input, and the IF…TH HE N statement to determine if the user is old enough to vote. 1 Launch Scratch. 2 Applying what you y have learned, select a sprite and a a backdrop for the project. 3 Add the when Green Flag clicked block to the sprite’s code. 4 Applying what you y have learned, add code that asks a the user for his or her age and waits for the answer to o be input. Recall that the user in np ut will be stored in the variable answer. 5 Drag the if then block b from the Control category, and a add it to the bottom of the code stack. 6 Code the expression IF answer 18 OR answer = 1 18 THEN, as shown. Scratch does not have a greater than or r equal to operator (≥) like the one e in math, so an equivalent expression must be coded in n two parts. You can hear the grea at er than or equal to relationship in the expression: to be e old enough to vote, the age enter re d must be greater than or equal to 18. when clicked ask What is your age? and wait if then or answer 18 answer = 18 Goodheart-Willcox Publisher Continued Copyright Goodheart-Willcox Co., Inc. 108 Introduction to Computer Science: Coding