Kotlin Comparison Operators

Kotlin Comparison Operators

Kotlin Comparison Operators are used to compare given two values. Like if one value is greater than, less than, equal to, greater than or equal to, or less than or equal to of another value.

In this tutorial, we shall learn about Comparison Operators, their symbols, and how to use them in Kotlin programs with help of examples.

Table for Comparison Operators

The following table covers Comparison Operators in Kotlin.

OperatorSymbolExampleDescription
Equal-to==x==yReturns true if x and y are equal in value, or else returns false.
Not-equal!=x!=yReturns true if x and y are not equal in value, or else returns false.
Greater-than>x>yReturns true if value in x is greater than that of in y, or else returns false.
Less-than<x<yReturns true if value in x is less than that of in y, or else returns false.
Greater-than or Equal-to>=x>=yReturns true if value in x is greater than or equal to that of in y, or else returns false.
Less-than or Equal-to<=x<=yReturns true if value in x is less than or equal to that of in y, or else returns false.

Example Kotlin Program for Comparison Operators

In the following program, we take two numbers: a, and b; and perform comparison operations on these numbers.

Kotlin Program

fun main() {
    var x = 5
    var y = 2

    println("x = $x\ny = $y")

    var result: Boolean

    // Equal-to
    result = x == y
    println("x == y   $result")

    // Not-equal
    result = x != y
    println("x != y   $result")

    // Greater-than
    result = x > y
    println("x > y    $result")

    // Less-than
    result = x < y
    println("x < y    $result")

    // Greater-than or Equal-to
    result = x >= y
    println("x >= y   $result")

    // Less-than or Equal-to
    result = x <= y
    println("x <= y   $result")
}

Output

x = 5
y = 2
x == y   false
x != y   true
x > y    true
x < y    false
x >= y   true
x <= y   false

Comparison Operators Tutorials

The following tutorials cover Comparison Operators in detail.