| () = () | |
() = () | |
| Category | Operators |
| Type | Boolean |
| Introduced in | 11Oct03 (0.x) |
The () = () block is an Operators block and a Boolean block. The block checks if the first value is equal to the second value. If the values are equal, the block returns true; otherwise, it returns false. This block is case-insensitive, meaning that capital and lowercase letters are treated the same.
In Scratch 1.3 and before, it only accepted numbers as values, though text variables could be used.[1]
This block has complicated behavior to handle the many kinds of values that can be compared. This sometimes results in unintuitive results, such as the comparison of long strings that are valid numbers. Some Scratchers have suggested for this to be fixed.[2]
Example Uses
If an action must be performed if or when a variable equals a certain number, this block can be used.
Some common uses for the () = () block:
- Pausing a script until a variable reaches a certain number
when flag clicked wait until <(score) = [10]> say [Good job!]
- Checking the answer
when green flag clicked
ask [Was my joke funny?] and wait
if <(answer) = [yes]> then
say [Thank you!] for (2) secs
end
- Checking if Boolean values are the same
if < <mouse down?> = <touching (sprite 1 v)?> > then
say [Either you are clicking and I am on sprite 1, or you are not clicking and I am not on sprite 1.]
end
- Comparing different values
if <(x position) = (size)> then
say [The sprite size and x position are equal.] for (5) seconds
else
say [The sprite size and x position are not equal.] for (5) seconds
end
Comparisons
The block first tries to cast the inputs to numbers. If both are not valid as double-precision floating-point numbers, they are compared as strings without case sensitivity.[3]
Comparing Text
Text comparisons are not case sensitive. Many believe this is intended to reduce the complexity of the platform and make it easier for young users to understand.
<[a] = [a]> // reports true <[a] = [A]> // reports true
Where case sensitivity is needed, other methods can be used that avoid comparing text in this block.
- Main article: Case Sensing
Comparing Numbers
This block will sometimes report that two very large numbers are true even if they are not.
<[100000000000000000] = [100000000000000001]> // reports true
This is because when the numbers are stored as double-precision floating-point numbers a loss of precision can occur, leading to the closest representation of both inputs being the same value. In very large numbers this could be the value Infinity.
This can be avoided by adding a letter at the beginning, which forces Scratch to only use a text comparison and not a number comparison:
<(join [a] (100000000000000000)) = (join [a] (100000000000000001))> // reports false
Workaround
- Main article: List of Block Workarounds
The block can be replicated with the following code:
<not < <(a) > (b)> or <(a) < (b) > > >
The following also works:
delete all of [list v] add (a) to [list v] if <[list v] contains (b)> then set [result v] to [true] else set [result v] to [false] end
See Also
References
- ↑ Scratch 1.3 Source Code: Block Spec
('%n = %n' #b #= #- #-) - ↑ topic:101150
- ↑ scratch-vm/src/util/cast.js
|
() + () • () - () • () * () • () / () • Pick Random () to () • () < () • () = () • () > () • () and () • () or () • Not () • Join ()() • Letter () of () • Length of () • () Mod () • Round () • () of ()More blocks...
|