Comparison Operators in Kotlin

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