All programming languages use these same statements. The actual coding of the statements may vary, but the format is the same. What are called instructions in other languages are called blocks in Scratch. The combination of these statements provides the logic for the c program. Comparison operators can be used in these statements to evaluate two numbers, variables, or expressions. In Scratch, the comparison operators are located in the Operators category. In some programming languages, comparison operators are known as relational operators. The comparison operators are shown in Figure 5-2. The result of the comparison is always true or false. In Figure 5-2, the variable age is compared to the number 5. Suppose the value stored in age is 13. In this case, age = 5 is false, age 5 is true, and age 5 is false. When a program makes a decision, it is based on a condition. In programming, a condition is an expression that can only be either true or false (or yes or no). These expressions are sometimes called Boolean expressions. This type of decision-making is called binary logic since there c are only two possible outcomes. Programmers use Boolean expressions in many practical ways. The alarm system in your home goes off if you do not enter the correct code. The programming on an e-commerce website checks to see if the credit card number you entered is valid. The light in your car’s fuel monitor glows when the fuel gets below a certain level. These are true-false or yes-no results after the evaluation of a condition. To create the logic for what a sprite will do when it reaches the edge of the stage, first code a statement to find the location of the sprite. In Scratch, each sprite has two local variables, x position and y position, that store the values for the sprite’s location. A comparison operator can then be used in a Boolean expression to check if the location is the edge of the screen. This may be IF the location is less than the stage edge, THEN keep moving or ELSE stop. Or, it may be IF the location equals the stage edge, THEN react. When the sprite reaches the edge, should it keep moving to the right? Should it bounce back? The way in which the sprite reacts is coded into the script. Comparing the location of a sprite to the edge of the stage is a common action. Therefore, Scratch has a unique block to perform this task. The block is if on edge, bounce. This block is simply a Boolean expression that evaluates the condition of whether or not the sprite is on the edge of the stage and then reverses its direction if true. It is a control statement even though it is found in the Motion category. British mathematician George Boole (1815–1864) laid the foundations of logic that are the basis of the information age. Bri t i sh m athe m at i c i a n George Boo l e ( 1815–1864) laid the f oun d ations o f l ogic that a r e the bas i s of the in f ormation age . FYI FYI Goodheart-Willcox Publisher Figure 5-2. There are three comparative operators in Scratch. The examples shown here compare the variable age to a value. Operator Operator Math Symbol Math Symbol Scratch Example Scratch Example Equals = age = 5 Is greater than age 5 Is less than age 5 Copyright Goodheart-Willcox Co., Inc. Chapter 5 Decisions 101
Previous Page Next Page