Comparison Operations

In order to work with numbers, you often need to mathematically manipulate them as demonstrated in the previous section. Doing so will ultimately leave you with a result. Typically, you will want to do something with this result once it has been calculated. For a simple application, all you may need to do is display its value. However, more often than not, you are going to end up using it to guide the execution of your application in some manner. For example, suppose you want to create a number guessing game that automatically generates a random number and then challenges the player to try to guess it. Once the random number is generated and stored in a variable, the player needs to be prompted to try to guess it (perhaps by clicking on one of 10 buttons with numbers printed on them). Once the player’s guess is captured, the application needs to compare the player’s guess against the value of the variable that stores the game’s random number to determine whether the player’s guess is correct. To facilitate this type of comparison operation, Scratch provides access to the three code blocks shown in Figure 8.4.

Figure 8.4. These code blocks provide the ability to compare any two numeric values.


The first and last code blocks shown in Figure 8.4 allow you to compare one value against a range of values. The first code block checks to see if the numeric value specified in its first input field is less than the value specified in its second input field. The third code block does the opposite, checking to see if the numeric value specified in its first input field is greater than the value specified in its second input field. The middle code block is used to determine if two values are equal.

To develop a better understanding of how to work with each of these three code blocks, let’s look at a few examples. In the first example, shown below, a script has been created that executes whenever the green flag button is clicked. When this happens, the value Count is set equal to 10. Next, a numbers block is embedded within a control block to set up a conditional test that evaluates the value assigned to Count and to execute the code block embedded within the control block in the event that the tested condition (Count equals 10) is true. Since this is the case, a text string of Hello! is displayed in a speech bubble.

Note

To prove that the embedded numbers code block is working as it is supposed to, you could change the value assigned to Count to something other than 10 and run the example again. Since the value assigned to Count no longer equals 10, the tested condition would evaluate as false, and the text message would not display.


In this next example, the numbers code block that tests for greater than conditions is used. Again, a script has been set up to execute whenever the green flag button is clicked. The value assigned to Count is then set to 1, and a control block is used to set up a loop that runs forever (until you provide a means for stopping its execution). A number of code blocks are embedded within the loop. The first block plays an audio file, and the second block pauses script execution for one second to allow Scratch time to finish playing the file. Another control block is then used to set up a conditional test that evaluates the value assigned to Count to see if it is greater than 2, and if it is, another control block is used to terminate the script’s execution. If the value assigned to Count is not greater than 2, then the last code block located at the bottom of the loop is executed, incrementing the value of Count by 1. The loop then repeats and executes again.

The first time the loop runs, the value assigned to Count is 1. The loop must iterate two times before the value of Count is set to 3, resulting in the termination of the script’s execution. Because of this, the audio file will play three times.

In this final example, shown next, the numbers code block that tests for less than conditions is used. Like the last two examples, this script is set up to execute whenever the green flag button is clicked. When this happens, the value of Count is set to 1. Next, a loop is set up that repeatedly executes as long as the value of Count is less than 15. Each time this test evaluates as true, three embedded code blocks are executed. The first code block moves the sprite 25 steps. The next code block increments the value assigned to Count by 1, and the last code block pauses script execution for one second.

The way this script is written, its loop will execute 14 times and will stop executing when the value of Count finally reaches 15.

Trick

While Scratch only supplies you with three code blocks for performing conditional tests (equality, greater than, and less than), most programming languages support three additional types of conditional tests, allowing you to perform the following comparison operations:

  • Greater than or equal to

  • Less than or equal to

  • Not equal to

Although Scratch does not provide equivalent code blocks, you can easily set up equivalent comparison tests by combining the three code blocks just discussed with Scratch’s logical comparison code blocks, as shown in Figure 8.5.

Figure 8.5. Creating customized logical comparisons.


The first combination of code blocks shown in Figure 8.5 creates a test that determines if the value assigned to a variable named Total is less than or equal to 10. This example is made up of five code blocks—two variable blocks, two numbers code blocks used to perform less than and equality comparisons, and another numbers block, which is used to tie everything together. The second combination of code blocks shown in Figure 8.5 is very similar and is designed to create a test that checks to see if the value assigned to Total is greater than or equal to 5. The last example is made up of three code blocks and is used to evaluate the values assigned to Total to determine to see if it is not equal to 3. You will learn more about code blocks that support logical comparisons in the next section.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.98.148