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