Kotlin Greater-than or Equal-to Operator
Kotlin Greater-than or Equal-to Operator returns true if left operand is greater than or equal to the right operand, or else it returns false.
Syntax
The syntax to use Greater-than or Equal-to operator is
value1 >= value2
where
value1
is left operand>=
is the symbol used for Greater-than or Equal-to operatorvalue2
is right operand
Return value
The above expression returns a boolean value of true if value1
is greater than or equal to value2
, or false otherwise.
Examples
Check if 45 is greater than or equal to 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 or Equal-to operator and check if value in n1
is greater than or equal to that of in n2
.
Kotlin Program
fun main() {
val n1 = 45
val n2 = 21
if (n1 >= n2) {
println("$n1 is greater than or equal to $n2.")
} else {
println("$n1 is not greater than or equal to $n2.")
}
}
Output
45 is greater than or equal to 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 45 is greater than or equal to 45
In the following program, consider that we are given two values in variables: n1
, and n2
, say 45
and 45
respectively. We have take same value in the two variables.
Kotlin Program
fun main() {
val n1 = 45
val n2 = 45
if (n1 >= n2) {
println("$n1 is greater than or equal to $n2.")
} else {
println("$n1 is not greater than or equal to $n2.")
}
}
Output
45 is greater than or equal to 45.
Since the value in n1
is equal to that of in n2
, n1 >= n2
returned true, and the if-block is run.
Check if 21 is greater than or equal to 45
In the following program, consider that we are given two values in variables: n1
, and n2
, say 21
and 45
respectively. The values are such that the Greater-than or Equal-to operator returns false
when the values are given as inputs in their respective order.
Kotlin Program
fun main() {
val n1 = 21
val n2 = 45
if (n1 >= n2) {
println("$n1 is greater than or equal to $n2.")
} else {
println("$n1 is not greater than or equal to $n2.")
}
}
Output
21 is not greater than or equal to 45.
Since the value in n1
is neither greater than nor equal to that of in n2
, n1 >= n2
returned false, and the else-block is run.