Cookie Preferences

We use cookies to enhance your experience. Choose your preference for cookie usage.

Essential cookies are required for basic functionality. Additional cookies help us improve our service and provide analytics.

View third-party services
  • Google Analytics: Website traffic analysis and conversion tracking
  • RudderStack: Analysis of user interactions with the website
  • Microsoft Clarity: User behavior analysis & session recordings
  • Facebook Pixel: Marketing analysis of user activity
KotlinBasic operations

Relational operations

List of relational operators

Kotlin provides six relational operators to compare numbers:

  • == (equal to)
  • != (not equal to)
  • > (greater than)
  • >= (greater than or equal to)
  • < (less than)
  • <= (less than or equal to)

The result of applying a relational operator to its operands takes the Boolean type (true or false) regardless of the types of operands.

Comparing integer numbers

Relational operators allow you to easily compare, among other things, two integer numbers. Here are some examples:

val one = 1
val two = 2
val three = 3
val four = 4

val oneIsOne = one == one // true

val res1 = two <= three // true
val res2 = two != four  // true
val res3 = two > four   // false
val res4 = one == three // false

Relational operators can be used in mixed expressions together with arithmetic operators. In such expressions, relational operators have a priority than arithmetic operators.

In the following example, first two sums will be calculated, and then they will be compared using the operator >.

val number = 1000val result = number + 10 > number + 9 // 1010 > 1009 is true

The result is true.

Joining relational operations using logical operators

In Kotlin, you cannot write an expression like this:
a <= b <= c

Instead, you should join two Boolean expressions using logical operators like || and &&.

For example, let's say we'd like to check the validity of the following expression:

100 < number < 200

To do that, we should write something like this:

number > 100 && number < 200
Also, we can put the different parts of the expression in parentheses:

(number > 100) && (number < 200)

But parentheses are not necessary here because relational operators have a higher priority than logical operators.

Logical operators allow you to join a sequence of relational operations into a single expression. This is a widely used trick in programming.

As a more complex example, let's consider a program that reads an integer number and checks if the number belongs to the range [100; 200], including 100 and 200 in the range.

import java.util.*

fun main(args: Array<String>) {
    val scanner = Scanner(System.`in`)

    val left = 100
    val right = 200
    val number = scanner.nextInt()

    val inRange = number >= left && number <= right // joining two expressions using AND

    println(inRange)
}

Here is a set of input values and the corresponding outputs: 50 - false; 99 - false; 100 - true; 199 - true; 200 - true; 201 - false; and so on.

In the following lessons, we will consider how to simplify the program above using some of Kotlin's features.
How did you like the theory?
Report a typo