- The correct title of this article is () < () (block). The Scratch Wiki uses this different title because of technical restrictions.
() < () | |
() \< () | |
Category | Operators |
Type | Boolean |
Introduced in | 11Oct03 |
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.
Example Uses
Some common uses for the () < () block are below:
set [i v] to [1] repeat until <<(i) > (length of [scores v])> or <(item (i) of [scores v]) < (score)>> change [i v] by (1) end insert (score) at (i) of [scores v]
- Evaluating numbers or letters
if <(score) < (5)> then say [Poor job.] for (1) secs stop [this script v] end if <(item (i) of [scores v]) < (10)> then say [Well done.] for (1) secs stop [this script v] end say [Great!] for (1) secs
- Comparing different variables
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
ask [Please rate my cookies from 1 to 10.] and wait repeat until <not <<(answer) < [1]> or <(answer) > [10]>>> ask [Please rate my cookies from 1 to 10.] and wait end
Comparison
Comparing Numbers
Numbers will be compared as expected unless they are much larger than their difference:
set [number 1 v] to [100] set [number 2 v] to [101] say <(number 1) < (number 2)> for (2) seconds // returns true set [number 1 v] to [100000000000000000] set [number 2 v] to [100000000000000001] say <(number 1) < (number 2)> for (2) seconds // returns false
This can be worked around only if the numbers are stored as strings, which happens if they are typed instead of calculated with Operators blocks.
This example compares numbers stored as strings that do not use a sign, leading zeros, a decimal point, nor scientific notation:
set [number 1 v] to [100000000000000000] ask [The answer is "100000000000000001"] and wait set [number 2 v] to (answer) if <(length of (number 1)) = (length of (number 2))> then say <(join (number 1) [a]) < (join (number 2) [a])> for (2) seconds // returns true else say <(length of (number 1)) < (length of (number 2))> for (2) seconds end
This works because it forces Scratch to do a text comparison instead of a number comparison, which is more accurate at the cost of a small amount of performance.
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 following code:
<(b) > (a)>
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 <(a) > (b)>>
See Also
() + () • () - () • () * () • () / () • Pick Random () to () • () < () • () = () • () > () • () and () • () or () • Not () • Join ()() • Letter () of () • Length of () • () Mod () • Round () • () of ()More blocks...
|