Kotlin supports a wide range of operators to perform various operations such as arithmetic, comparison, logic, and assignment. Operators are symbols that tell the compiler to perform specific mathematical or logical operations.
Used for basic mathematical operations:
fun main() { val a = 10 val b = 3 println("a + b = ${a + b}") println("a - b = ${a - b}") println("a * b = ${a * b}") println("a / b = ${a / b}") println("a % b = ${a % b}") }
Used to compare two values:
fun main() { val x = 5 val y = 8 println(x == y) println(x != y) println(x < y) println(x >= y) }
Used to combine multiple conditions:
fun main() { val a = true val b = false println(a && b) // false println(a || b) // true println(!a) // false }
Used to assign or update values:
fun main() { var num = 10 num += 5 // 15 num -= 3 // 12 num *= 2 // 24 num /= 4 // 6 println(num) }
Used to increment, decrement or negate values:
fun main() { var a = 4 println(++a) // 5 println(--a) // 4 println(+a) // 4 println(-a) // -4 }
Next Topic: Control Flow in Kotlin →
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!