Continued 7 Drag the say block o from the Looks category, and drop d it into the space within the if then block, as shown n. Change the text to “You are old enough to vote.” If the user enters any number greater than or equal to 18, the condition n is true. If the user enters anything else, the condition is false. If the condition is true, Scratch a will process the say block within the if then block. when clicked ask What is your age? and wait if then or answer 18 answer = 18 say You are old enough to vote. Goodheart-Willcox Publisher 8 Click the Green Flag button to run the program. Run the program several times to test it, entering differen nt values each time. Notice there is alternative output if the user enters 17 or less. 9 Save the project t as HOE05-03A in your working fo ol der. Try It! Students under the t age of 13 are given a discount t on movie tickets. Create a project that determines if the use er is allowed to have a reduced tic ck et price. What condition will be tested? What is the Boolean expression? Include code that tel ll s the user to press any key to end the program and waits for o the user to do so. Save the proje ec t as TI05-03A in your working folder. Green Flag IF…THEN…ELSE Block The IF…THEN statement does not provide an alternative for the program to follow if the condition is false. The IF…THEN…ELSE statement allows the code to branch in one of two ways, as shown in Figure 5-6. if else then Condition to test is inserted here Code to execute when the condition is true is inserted here Code to execute when the condition is false is inserted here Goodheart-Willcox Publisher Figure 5-6. The if then else block in Scratch provides a way for the code to branch based on a condition. Copyright Goodheart-Willcox Co., Inc. Chapter 5 Decisions 109
Previous Page Next Page