Relational Comparison Operators in Kotlin

Hey Developer, welcome back to Kotlin Operators tutorial, In this post, we will discuss about Relational Comparison Operators in Kotlin.

Relation comparison operators are making a comparison between two variables. Hence, the comparison operator helps us to make a decision in the programs and it shows the relation and compare between operands. While making a project most of the cases Relation comparison operators are used in control flow statements such as if, when, and loop expression.

Relational Comparison Operators in Kotlin

(Operator, Name and Expression)

Operator

>
<
>= 
<=  

Name

greater than operator 
less than operator
greater than or equal to operator
less than or equal to operator 

Expression

x > y
x < y
 x >= y 
 x =< y 

These operator is used for comparing two operands or variable, and one more thing Relation comparison operator always return Boolean, it will be true or false. Now let see the types of Relation Comparison Operator with expression:

Relation comparison Operator with Expression

Greater than Operator a > b
Less than Operator a < b

Greater than and equal to Operator a >= b
Less than and equal to Operator a < = b

Basically there are 4 different types of special character, which is used to compare two variable. let’s discuss one by one.

1. Greater than Operator

fun main() {
var x = 20
var y = 30

// check if value of x greater than y,
// if the condition is true then if block of code to be executed.
// otherwise else block of code to be executed.
// you can assign the result of an if-else expression to a variable. Ex: maxNumber.
var maxNumber= if(x > y) {
    println("x is greater than y")
    x
}
else {
    println("y is greater than x")
    y
}
println("maxNumber is $maxNumber")  //  result
}

Run the program and Output will be:

y is greater than x
maxNumber is 30

2. Less than Operator

fun main() { var x = 20
var y = 30 // check if value of x less than y,

// if the condition is true then if block of code to be executed.
// otherwise else block of code to be executed.
if(x < y) {
println("x is less than y")
}
else {
println("y is less than x")
}
}

Run the program and Output will be:

x is less than y

3. Greater than and Equal to Operator

fun main() {
var x = 20
var y = 20

// check if value of x greater than and equal to y,
// if the condition is true then if block of code to be executed.
// otherwise else block of code to be executed.
if(x >= y) {
    println("x is greater than and equal to y")
}
else {
    println("x is not greater than and equal to y")
}
}

Run the program and Output will be:

x is greater than and equal to y

4. Less than and Equal to Operator

fun main() { 

var x = 30
var y = 20 // check if value of x less than and equal to y,

// if the condition is true then if block of code to be executed.
// otherwise else block of code to be executed.
if(x <= y) {
println("x is less than and equal to y")
}
else {
println("x is not less than and equal to y")
}
}

Run the program and Output will be:

x is not less than and equal to y

Recommended Reading:

If you have any questions regarding this post. Feel free to comment and share the post with other developers, who want to learn Kotlin and Android App development.

Happy Learning!!!

Leave a Reply