Kotlin Operators – Arithmetic, Comparison, and Logical Operators Explained

Introduction

Operators in Kotlin are special symbols used to perform operations on variables and values. They are essential for any program as they help manipulate data and control program flow.

In this post, we’ll explore the most common operators used in Kotlin:
Arithmetic Operators
Comparison Operators
Logical Operators
Assignment Operators
Unary Operators

By the end of this tutorial, you’ll be comfortable using these operators to perform different operations in Kotlin.


1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations.

✅ Addition (+)

Used to add two numbers.

val sum = 10 + 5
println(sum) // Output: 15

✅ Subtraction (-)

Used to subtract one number from another.

val difference = 10 - 5
println(difference) // Output: 5

✅ Multiplication (*)

Used to multiply two numbers.

val product = 10 * 5
println(product) // Output: 50

✅ Division (/)

Used to divide one number by another.

val quotient = 10 / 5
println(quotient) // Output: 2

✅ Modulus (%)

Used to find the remainder of a division.

val remainder = 10 % 3
println(remainder) // Output: 1

Example: Calculating the Area of a Rectangle

val length = 10
val width = 5
val area = length * width
println("Area of rectangle: $area") // Output: 50

2. Comparison Operators

Comparison operators are used to compare two values and return a Boolean value (true or false).

✅ Equal to (==)

Used to check if two values are equal.

val isEqual = 10 == 5
println(isEqual) // Output: false

✅ Not equal to (!=)

Used to check if two values are not equal.

val isNotEqual = 10 != 5
println(isNotEqual) // Output: true

✅ Greater than (>)

Used to check if one value is greater than another.

val isGreaterThan = 10 > 5
println(isGreaterThan) // Output: true

✅ Less than (<)

Used to check if one value is less than another.

val isLessThan = 10 < 5
println(isLessThan) // Output: false

✅ Greater than or equal to (>=)

Used to check if one value is greater than or equal to another.

val isGreaterOrEqual = 10 >= 10
println(isGreaterOrEqual) // Output: true

✅ Less than or equal to (<=)

Used to check if one value is less than or equal to another.

val isLessOrEqual = 5 <= 10
println(isLessOrEqual) // Output: true

Example: Checking if a Number is Between Two Values

val number = 10
val isBetween = number >= 5 && number <= 15
println(isBetween) // Output: true

3. Logical Operators

Logical operators are used to combine multiple Boolean expressions.

✅ AND (&&)

Used to check if both expressions are true.

val isAdult = true
val hasTicket = true
val canEnter = isAdult && hasTicket
println(canEnter) // Output: true

✅ OR (||)

Used to check if at least one expression is true.

val isAdult = false
val hasTicket = true
val canEnter = isAdult || hasTicket
println(canEnter) // Output: true

✅ NOT (!)

Used to negate a Boolean expression.

val isAdult = true
val isNotAdult = !isAdult
println(isNotAdult) // Output: false

Example: Checking Multiple Conditions for User Login

val isRegistered = true
val isPasswordCorrect = false
val canLogin = isRegistered && isPasswordCorrect
println(canLogin) // Output: false

4. Assignment Operators

Assignment operators are used to assign values to variables.

✅ Simple Assignment (=)

Assigns a value to a variable.

val age = 30

✅ Add and Assign (+=)

Adds a value to the variable and assigns it back.

var score = 10
score += 5
println(score) // Output: 15

✅ Subtract and Assign (-=)

Subtracts a value from the variable and assigns it back.

var score = 15
score -= 5
println(score) // Output: 10

✅ Multiply and Assign (*=)

Multiplies a value to the variable and assigns it back.

var score = 10
score *= 3
println(score) // Output: 30

✅ Divide and Assign (/=)

Divides the variable by a value and assigns it back.

var score = 30
score /= 5
println(score) // Output: 6

5. Unary Operators

Unary operators are used to operate on a single value.

✅ Unary Plus (+)

Indicates a positive value (often optional).

val number = +10
println(number) // Output: 10

✅ Unary Minus (-)

Negates the value.

val number = -10
println(number) // Output: -10

✅ Increment (++)

Increases a value by one.

var number = 5
number++
println(number) // Output: 6

✅ Decrement (--)

Decreases a value by one.

var number = 5
number--
println(number) // Output: 4

Conclusion

In this post, you learned:
✅ The most common Kotlin operators: Arithmetic, Comparison, Logical, Assignment, and Unary.
✅ How to use these operators to manipulate data and control program flow.

🎯 Next Post: Kotlin Control Flow – Conditionals and Loops Explained

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *