Scala Comparators

A few new concepts are needed for this SKB.

First, what is a Boolean? A Boolean can only take two values: true or false. It is the result of a mathematical question. For instance, is 2 less than 5 (2 < 5), it would returns "yes" (true).

And from previous SKB, you might have noticed that assert tests if what is inside is true.

In Scala, and in the majority of other programming languages, you have a series of built-in comparators for mathematics:

  • == : for equality. Some tips here:
    • Notice that there are two ==. With only one =, it would be an assignation (Like in: val a = 2).
    • In your projects as a software engineer, always be careful about comparing numbers with decimals (Like 1.5632426546, they are called Double or Float) because the computer will round those numbers. So instead of a == b, you should be doing something like: Math.abs(a - b) < 0.01.
  • != : Not equal
  • > : Greater than
  • < : Less than
  • >= : Greater than or equal
  • <= : Less than or equal

Reveal more information and clues
Load Exercise