Kotlin Operators

Operators are symbols that perform actions on values. Kotlin provides operators for arithmetic, comparison, assignment, logical operations, and more. Understanding operators is fundamental to writing any meaningful program.

Arithmetic Operators


Operator │ Symbol │ Example    │ Result
─────────┼────────┼────────────┼────────
Add      │ +      │ 10 + 3     │ 13
Subtract │ -      │ 10 - 3     │ 7
Multiply │ *      │ 10 * 3     │ 30
Divide   │ /      │ 10 / 3     │ 3 (Int division)
Modulo   │ %      │ 10 % 3     │ 1 (remainder)
val a = 17
val b = 5

println(a + b)   // 22
println(a - b)   // 12
println(a * b)   // 85
println(a / b)   // 3  (integer part only)
println(a % b)   // 2  (remainder after dividing)

Comparison Operators

Comparison operators compare two values and return a Boolean result:

val x = 10
val y = 20

println(x == y)    // false  (equal to)
println(x != y)    // true   (not equal to)
println(x > y)     // false  (greater than)
println(x < y)     // true   (less than)
println(x >= 10)   // true   (greater than or equal)
println(x <= 10)   // true   (less than or equal)

Assignment Operators

var score = 100

score += 50    // same as: score = score + 50 → 150
score -= 30    // same as: score = score - 30 → 120
score *= 2     // same as: score = score * 2  → 240
score /= 4     // same as: score = score / 4  → 60
score %= 7     // same as: score = score % 7  → 4

Increment and Decrement

var count = 5

count++        // post-increment: use current value, then add 1
count--        // post-decrement: use current value, then subtract 1
++count        // pre-increment: add 1, then use the new value
--count        // pre-decrement: subtract 1, then use the new value

Pre vs Post — the difference matters

var n = 5
println(n++)    // prints 5, then n becomes 6
println(n)      // 6

var m = 5
println(++m)    // m becomes 6 first, then prints 6
println(m)      // 6

Logical Operators

val hasAccess  = true
val isVerified = false

println(hasAccess && isVerified)    // false (AND)
println(hasAccess || isVerified)    // true  (OR)
println(!hasAccess)                 // false (NOT)

Operator Precedence

Kotlin evaluates operators in a specific order, just like the BODMAS rule in mathematics:


Highest priority (evaluated first)
  1. Unary: ++ -- ! -
  2. Multiplication: * / %
  3. Addition: + -
  4. Comparison: < > <= >=
  5. Equality: == !=
  6. Logical AND: &&
  7. Logical OR: ||
Lowest priority (evaluated last)
val result = 2 + 3 * 4        // 14, not 20 (multiplication first)
val result2 = (2 + 3) * 4    // 20 (parentheses override precedence)

in and !in Operators

The in operator checks whether a value exists inside a range or collection:

val grade = 75

println(grade in 60..100)   // true  (in range 60 to 100)
println(grade !in 0..59)    // true  (not in failing range)

val fruits = listOf("apple", "mango", "grape")
println("mango" in fruits)  // true
println("banana" in fruits) // false

is and !is Operators

The is operator checks whether a value is of a specific type:

val item: Any = "Hello"

println(item is String)    // true
println(item is Int)       // false
println(item !is Int)      // true

Practical Example: Score Classifier

fun main() {
    val score = 82
    val bonus = 5
    val total = score + bonus

    val passed = total >= 50
    val distinction = total >= 75 && total <= 100

    println("Score : $score")
    println("Bonus : +$bonus")
    println("Total : $total")
    println("Passed: $passed")
    println("Distinction: $distinction")
}

Output:

Score : 82
Bonus : +5
Total : 87
Passed: true
Distinction: true

Leave a Comment

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