Comparison Operators in Kotlin
On this page (10sections)
Definition
Comparison operators are used for comparing numbers. There are four comparison operators in Kotlin. They are
- Greater than Operator (>)
- Less than Operator (<)
- Greater than and equal to Operator (>=)
- Less than and equal to Operator (<=)
Syntax
1. Greater than Operator
if(number1 > number2){
//Block of Code
}
For example,
var num1 = 100
var num2 = 5
if(num1 > num2){
//Block of Code
}
2. Less than Operator
if(number1 < number2){
//Block of Code
}
For example,
var num1 = 100
var num2 = 5
if(num1 < num2){
//Block of Code
}
3. Greater than and Equal to Operator
if(number1 >= number2){
//Block of Code
}
For example,
var num1 = 100
var num2 = 5
if(num1 >= num2){
//Block of Code
}
4. Less than and Equal to Operator
if(number1 < number2){
//Block of Code
}
For example,
var num1 = 100
var num2 = 5
if(num1 <= num2){
//Block of Code
}
Comparison Operator Example Program in Kotlin
//Comparison Operator Example Program in Kotlin
//Operator Kotlin Programs, Basic Kotlin Program
fun main(args: Array<String>) {
val num1 = 100;
val num2 = 5;
//Greater than Operator
if (num1 > num2) {
println("$num1 is greater than $num2")
}
//Less than Operator
if (num2 < num1) {
println("$num2 is lesser than $num1")
}
//Greater than and Equal to Operator
if (num1 >= 100) {
println("$num1 is equal to 100")
}
//Less than and Equal to Operator
if (num2 <= 5) {
println("$num2 is equal to 5")
}
}
Sample Output
100 is greater than 5
5 is lesser than 100
100 is equal to 100
5 is equal to 5
How It Works
This Kotlin program demonstrates Comparison Operators. It first prepares the data it needs, then uses conditional logic to decide the result, and finally prints the output shown in the Sample Output above.
- Declare the variables that hold the program’s data.
- Use conditional statements to handle the different cases.
- Print the final result to the console so you can compare it with the sample output.
Try changing the input values and re-running the program to see how the output changes — this is the fastest way to understand how the logic behaves.
Frequently Asked Questions
What is Comparison Operators in Kotlin?
Comparison operators are used for comparing numbers.
How do I run this Kotlin example?
Run it in IntelliJ IDEA or Android Studio, or compile from the command line with `kotlinc file.kt -include-runtime -d file.jar` and run `java -jar file.jar`.
How can I practice Comparison Operators?
Copy the example into IntelliJ IDEA or Android Studio, run it, then change the values or add print statements to see how the output changes.