Relational Operators

In the following lesson, you will be introduced to relational operators.

Press + to interact
Relational operators
Relational operators

Relational operators are operators that perform operations which compare operands of numeric types. For example, less than and greater than. Below is a list of the relational operators supported by Scala.

Operator Use
> Checks if the value of the left operand is greater than the value of the right operand
< Checks if the value of the left operand is less than the value of the right operand
>= Checks if the value of the left operand is greater than or equal to the value of the right operand
<= Checks if the value of the left operand is less than or equal to the value of the right operand
!= Checks if the values of the two operands are equal or not

Relational Operators yield a Boolean type result.

Taking the first operand to be 1010 and the second operand to be 77, let’s look at an example for each operator.

Press + to interact
val operand1 = 10
val operand2 = 7
println(operand1 > operand2)
println(operand1 < operand2)
println(operand1 >= operand2)
println(operand1 <= operand2)
println(operand1 != operand2)

We can also use relational operators on non-integer literals such as character literals. In this case, the compiler will compare the Unicode of the characters. Let’s look at an example where the first operand is a and the second operand is b. As a comes before b alphabetically, a would be less than b.

Press + to interact
val operand1 = 'a'
val operand2 = 'b'
println(operand1 > operand2)
println(operand1 < operand2)
println(operand1 >= operand2)
println(operand1 <= operand2)
println(operand1 != operand2)

That sums up relational operators, let’s move on to logical operators in the next lesson.