Kotlin Less-than

Kotlin Less-than Operator

Kotlin Less-than Operator returns true if left operand is less than the right operand, or else it returns false.

Syntax

The syntax to use Less-than operator is

value1 < value2

where

  • value1 is left operand
  • < is the symbol used for Less-than operator
  • value2 is right operand

Return value

The above expression returns a boolean value of true if value1 is less than value2, or false otherwise.

Examples

Check if 21 is less than 45

In the following program, consider that we are given two values in variables: n1, and n2, say 21 and 45 respectively. We shall use Less-than operator and check if value in n1 is less than that of in n2.

Kotlin Program

fun main() {
    val n1 = 21
    val n2 = 45

    if (n1 < n2) {
        println("$n1 is less than $n2.")
    } else {
        println("$n1 is not less than $n2.")
    }
}

Output

21 is less than 45.

Since the value in n1 is less than that of in n2, n1 < n2 returned true, and the if-block is run.

Check if 45 is less than 21

In the following program, consider that we are given two values in variables: n1, and n2, say 45 and 21 respectively. We shall use Less-than operator and check if value in n1 is less than that of in n2.

Kotlin Program

fun main() {
    val n1 = 45
    val n2 = 21

    if (n1 < n2) {
        println("$n1 is less than $n2.")
    } else {
        println("$n1 is not less than $n2.")
    }
}

Output

45 is not less than 21.

Since the value in n1 is not less than that of in n2, n1 < n2 returned false, and the else-block is run.