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
Kotlin Operators
- Kotlin Operators Overview
- Arithmetic Operators (Mathematical Operators) in Kotlin
- Assignment Operators and Augmented Assignment Operators in Kotlin
- Unary Operators (Sign, Inverts, Increment, and Decrement) in Kotlin
- Logical Operators in Kotlin
- Equality Operators (==, !=) and Referential equality Operators (===, !==) in Kotlin
- Comparison Operators in Kotlin
- In Operator (in and !in) in Kotlin
- Is Operator (is and !is) in Kotlin
- Indexed Access Operator [, ] in Kotlin
- Not Null Assertion Operator in Kotlin
- Safe Call Operator (?.) in Kotlin
- Elvis Operator (?:) in Kotlin
- Range Operator (..) in Kotlin
Read More Articles
- Declare Variables In Kotlin
- Read Data Input using Scanner in Kotlin
- Double Data type Usage and Type Conversion in Kotlin
- print and println Data Output in Kotlin
- Arithmetic Operators (Mathematical Operators) in Kotlin
- Unary Operators (Sign, Inverts, Increment, and Decrement) in Kotlin
- Equality Operators (==, !=) and Referential equality Operators (===, !==) in Kotlin
- Printing Variables and Values in Kotlin
- Float Data type Usage and Type Conversion in Kotlin
- Long Data type Usage and Type Conversion in Kotlin
- Comparison Operators in Kotlin
- Byte Data type Usage and Type Conversion in Kotlin
- Is Operator (is and !is) in Kotlin
- In Operator (in and !in) in Kotlin
- Char Data type Usage and Type Conversion in Kotlin
- Read Data Input from Command Line in Kotlin
- Assignment Operators and Augmented Assignment Operators in Kotlin
- Indexed Access Operator [, ] in Kotlin
- Read String Data Input in Kotlin
- Short Data type Usage and Type Conversion in Kotlin
- Boolean Data type Usage and Type Conversion in Kotlin
- Elvis Operator (?:) in Kotlin
- Logical Operators in Kotlin
- Repeat and Its Usage in Kotlin
- Safe Call Operator (?.) in Kotlin