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

True

Comments

  1. Comparison operators can be chained, so 0 <= 73 <= 100 reads as “0 is at most 73, and 73 is at most 100”. The <= operator allows the endpoints, so a score of exactly 0 or 100 would also count as valid.

  2. Both parts are true, so the whole expression is True. Note that the middle value 73 is written only once, even though it is compared with both neighbours.

  3. 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.

Course navigation

Chained comparison   |   Quiz — Precedence