Performing Logical Comparisons

In addition to code blocks designed to perform mathematical and comparison operations, Scratch also provides access to three code blocks that support logical comparison operations. These code blocks are shown in Figure 8.6.

Figure 8.6. Using these code blocks, you can perform more complex comparison operations.


The first code block is used to test two different sets of values to determine if both are true. The second code block is used to test two different sets of values to determine if at least one is true. And the last code block lets you evaluate two values to determine if the tested condition is false (not true).

To help you better understand how to work with all three of these code blocks, let’s review a few examples. The first example, shown next, is a script that executes whenever the green flag button is clicked. When this occurs, the value assigned to the variable Count is set to 50. Next, a control code block is used to analyze the value assigned to Count. If the value of Count is less than 100 and also greater than 10, then the end statement embedded within the control block is executed. However, if both tested conditions evaluate as false, the embedded code block is not executed.

Note

Scratch is very flexible in its support for numbers blocks. For example, if you prefer, you could swap the order in which the two embedded numbers blocks occur (e.g., checking to see that Count is greater than 10 before checking to make sure that Count is also less than 100), and the results would be the same.


This next example is very similar to the previous example, except that instead of ensuring that both tested conditions evaluate as being true, the script has been modified so that only one of the tested conditions has to be evaluated as true in order for the embedded code block to be executed.

This final example shows a script that performs a negative test, checking to see if two values are not equal instead of checking to see if they are equal. As a result, if the value assigned to Count is not equal to 50, which it is not, the code block embedded within the control block is executed.

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

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