Page 149 - Learn To Program With Scratch
P. 149
Knowing this information, you can test the player’s guess using the fol-
lowing set of conditionals:
IF (answer = secretCode), then say Correct
IF (answer > secretCode), then say Before <answer>
IF (answer < secretCode), then say After <answer>
A conditional is a statement of the form, “If the condition is true, then
take this action.” In the next section, I’ll teach you how to implement con-
ditionals in Scratch, but let’s explore relational operators a bit further with
our code-guessing game first.
What if the secret code contains more than one letter? For example, the
player might need to guess the name of an animal. Can you still use Scratch’s
relational operators to do the comparison? Luckily, the short answer is yes:
You can use Scratch’s relational operators to compare strings. But how does
Scratch process a comparison like elephant > mouse? The examples in
Figure 6-2 illustrate the result of comparing strings.
Figure 6-2: Using relational operators to compare u identical strings, v strings
that differ only in case, w one string to another that contains extra spaces, and
x strings that vary according to the dictionary order of their letters
A careful study of Figure 6-2 shows the following:
• Scratch compares strings irrespective of their case. The strings
“HELLO” and “hello” in v, for example, are considered equal.
• Scratch counts white spaces in its comparison. The string “ HELLO ”,
which starts and ends with a single space, is not the same as the string
“HELLO” w.
• When comparing the strings “ABC” and “ABD”, as in x, Scratch first
considers the first character in the two strings. Since they are the same
(the letter A in this case), Scratch examines the second character in
both strings. Since this character is also the same in the two strings,
Scratch moves on to examining the third character. Since the letter
C is less than the letter D (that is, C comes before D in the alphabet),
Scratch considers the first string to be less than the second string.
Making Decisions 127
www.it-ebooks.info

