- The correct title of this article is () < (). The Scratch Wiki uses a different title because of technical restrictions.
| () < () | |
() \< () | |
| Category | Operators |
| Type | Boolean |
| Introduced in | 11Oct03 (0.x) |
The () < () block is an Operators block and a Boolean block. The block reports true if the first value is less than the second value and false otherwise.
This block can compare both numbers and strings, which are ordered alphabetically. In Scratch 1.3 and previous versions, it only accepted numbers.
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.
Example Uses
Some common uses for the () < () block are below:
if <(score 1) < (score 2)> then
say [Player 2 wins!]
else
say [Player 1 wins!]
end
- Checking that a value is within a given range
<<[0] < (score)> and <(score) < [10]>> // true if score is between 0 and 10
Comparison
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.[1]
Comparing Numbers
Numbers will be compared as expected unless they are very large:
<[100] < [101]> // returns true <[100000000000000000] < [100000000000000001]> // returns false
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])> // returns true
Comparing Text
Text is compared alphabetically:
<[a] < [b]> // returns true <[b] < [a]> // returns false
The Empty Value
The empty value will always be considered smaller than other values:
<[] < [10]> // returns true <[10] < []> // returns false
Workaround
- Main article: List of Block Workarounds
The block can be replicated with the greater than operator and swapping the inputs:
<[4] < [5]> // returns true <[5] > [4]> // returns true
Less Than Or Equal To
Sometimes it is necessary to know if a value is less than or equal to another value, but there is no block to do so. This can be done as follows:
<not <[4] > [5]>> // 4 ≤ 5 returns true
See Also
References
|
() + () • () - () • () * () • () / () • Pick Random () to () • () < () • () = () • () > () • () and () • () or () • Not () • Join ()() • Letter () of () • Length of () • () Mod () • Round () • () of ()More blocks...
|