Scala relational operations in detail

 198 total views,  1 views today

Welcome to Scala series. In this tutorial, you will learn about Scala relational operations in detail.

Scala relational operations

Below are the relation operations supported in Scala

== (Check whether two values are equal or not.It returns the result as boolean. If equal returns true, else returns false.)

!= (Check whether the first value is not equal to second value.Returns the result as boolean)

> (Check whether the first value is greater than the second value)

< (Check whether the first value is lesser than second value)

>= (Check whether the first value is greater than or equal to second value)

<= (Check whether the first value is lesser than or equal to second value)

The result of all relational operations is a Boolean value.

Let us see an example for each operation.

Scala relational operations examples

== (equal to) example

var input1 = 10;
var input2 = 10;

var result = input1 == input2

println(result)

result = input1.==(input2)

println(result)

Output

true
true

!= (not equal to) example

def main(args: Array[String]): Unit = {
    var input1 = 10;
    var input2 = 20;

    var result = input1 != input2

    println(result)

    result = input1.!=(input2)

    println(result)

  }

Output

true
true

> (greater than) example

def main(args: Array[String]): Unit = {
    var input1 = 40;
    var input2 = 20;

    var result = input1 > input2

    println(result)

    result = input1.>(input2)

    println(result)

  }

Output

true
true

< (lesser than) example

  def main(args: Array[String]): Unit = {
    var input1 = 40;
    var input2 = 80;

    var result = input1 < input2

    println(result)

    result = input1.<(input2)

    println(result)

  }

Output

true
true

>= (greater than or equal to) example

  def main(args: Array[String]): Unit = {
    var input1 = 40;
    var input2 = 20;

    var result = input1 >= input2

    println(result)

    result = input1.>=(input2)

    println(result)

  }

Output

true
true

<= (lesser than or equal to) example

 def main(args: Array[String]): Unit = {
    var input1 = 40;
    var input2 = 20;

    var result = input1 <= input2

    println(result)

    result = input1.<=(input2)

    println(result)

  }

Output

false
false

References Official documentation

Thanks for reading. Please leave a comment and give us a thumbs up!.

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.