Course of Raku / Advanced / Operators / Review of operator behaviour / Exercises / Chained comparison
Solution: Chained comparison
Here is a possible solution to the task.
Code
say 0 <= 73 <= 100;🦋 You can find the source code in the file chained-comparison.raku.
Output
TrueComments
Comparison operators can be chained, so
0 <= 73 <= 100reads as “0 is at most 73, and 73 is at most 100”. The<=operator allows the endpoints, so a score of exactly0or100would also count as valid.Both parts are true, so the whole expression is
True. Note that the middle value73is written only once, even though it is compared with both neighbours.Without the chaining feature, you would have to spell out both comparisons and join them with
&&, repeating the middle value:0 <= 73 && 73 <= 100. The chained form says the same thing more concisely.