From Test-Scratch-Wiki
- The correct title of this article is () < (). The change of name is due to technical restrictions.
() < () | |
Category | Operators |
Type | Boolean |
The () < () block is an Operators block and a Boolean block. The block checks if the first value is less than the second value. If it is less, the block returns true; if not, it returns false.
This block works with letters too, as well as numbers. In Scratch, letters at the top of the alphabet (e.g. a, b, c) are worth less than letters at the end (e.g. x, y, z). In Scratch 1.3 and previous versions, it only accepted numbers.
Example Uses
Some common uses for the () < () block are below:
- If a group must be arranged in size, this block can easily do the job by iterating through a list and checking if each value is less than the rest until placement is detected. For example, if a list is supposed to be sorted from greatest to least, the following script can accomplish this:
set [i v] to [1] //start at the front repeat (length of [numbers v]) set [i2 v] to [1] repeat until <<(item (i) of [numbers v]) < (item (i2) of [numbers v])> or <(i2) > (length of [numbers v]) change [i2 v] by (1) end insert (item (i) of [numbers v]) at ((i2) + (1)) of [numbers v] if <(i2) < ((i) + (1))> then delete ((i) + (1)) of [numbers v] else delete (i) of [numbers v] end change [i v] by (1) end
- Arranging a set of numbers/letters
if <<(score) < (item (1 v) of [Top Scores v])> and <(score) > (item (2 v) of [Top Scores v])>> then insert (score) at (2 v) of [list v] end
- Evaluating numbers/letters
set [i v] to [1] delete [all v] of [evaluation v] repeat (length of [scores v]) if <(item (i) of [scores v]) < (5)> then //if the score is lower than 5 insert [okay] at (i) of [evaluation v] //tells how the score is else if <(item (i) of [scores v]) < (10)> then insert [good] at (i) of [evaluation v] else insert [great!] at (i) of [evaluation v] end end change [i v] by (1)
- Comparing different variables (e.g. comparing two characters' health in a game)
if <(1 Health) < (2 Health)> then say [Player 2 wins] else say [Player 1 wins] end
- Asking for an opinion on a scale of 1 to 10
set [confirmed v] to [0] //stores yes or no is the answer is complete and acceptable repeat until <(confirmed) = [1]> ask [How good are my cookies?] and wait if <<(answer) < (1)> or <(answer) > (10)>> then say [The response must be from 1 through 10] for (2) secs else set [confirmed v] to [1] end end
Workaround
- Main article: List of Block Workarounds
The block can be replicated with the following code:
<not <<(a) > (b)> or <(a) = (b)>>>
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. However, it can be worked around in two ways:
<not <(a) > (b)>> <<(a) < (b)> or <(a) = (b)>>
The first workaround is more efficient.
See Also
() + ()
• () - () • () * () • () / () • Pick Random () to () • () < () • () = () • () > () • () and () • () or () • Not () • Join ()() • Letter () of () • Length of () • () Mod () • Round () • () of ()More blocks... |