Kotlin Greater-than Operator
Kotlin Greater-than Operator returns true if left operand is greater than the right operand, or else it returns false.
Syntax
The syntax to use Greater-than operator is
value1 > value2
where
value1
is left operand<
is the symbol used for Greater-than operatorvalue2
is right operand
Return value
The above expression returns a boolean value of true if value1
is greater than value2
, or false otherwise.
Examples
Check if 45 is greater 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 Greater-than operator and check if value in n1
is greater than that of in n2
.
Kotlin Program
fun main() {
val n1 = 45
val n2 = 21
if (n1 > n2) {
println("$n1 is greater than $n2.")
} else {
println("$n1 is not greater than $n2.")
}
}
Output
45 is greater than 21.
Since the value in n1
is greater than that of in n2
, n1 > n2
returned true, and the if-block is run.
Check if 21 is greater 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 Greater-than operator and check if value in n1
is greater than that of in n2
.
Kotlin Program
fun main() {
val n1 = 21
val n2 = 45
if (n1 > n2) {
println("$n1 is greater than $n2.")
} else {
println("$n1 is not greater than $n2.")
}
}
Output
21 is not greater than 45.
Since the value in n1
is not greater than that of in n2
, n1 > n2
returned false, and the else-block is run.