Wednesday 6 December 2017

Kotlin Tutorial in Hindi | Comparison Operators



Here's a table of equality and comparison operators, their meaning, and corresponding functions:
OperatorMeaningExpressionTranslates to
>greater thana > ba.compareTo(b) > 0
<less thana < ba.compareTo(b) < 0
>=greater than or equals toa >= ba.compareTo(b) >= 0
<=less than or equals toa < = ba.compareTo(b) <= 0
==is equal toa == ba?.equals(b) ?: (b === null)
!=not equal toa != b!(a?.equals(b) ?: (b === null))
Comparison and equality operators are used in control flow such as if expressionwhen expression, and loops.

Example: Comparison and Equality Operators

fun main(args: Array<String>) {

    val a = -12
    val b = 12

    // use of greater than operator
    val max = if (a > b) {
        println("a is larger than b.")
        a
    } else {
        println("b is larger than a.")
        b
    }

    println("max = $max")
}
When you run the program, the output will be:
b is larger than a.
max = 12

No comments:

Post a Comment