Labs ICT
Pro Login

Assignment Operators

Assignment operators are shortcuts for updating a variable's value based on its current value. Instead of writing x = x + 5, you can write x += 5.

The basic assignment =

You already know this one. It assigns a value to a variable.


fun main() {
  var x = 10
  println(x)
}
    

Compound assignments

These combine an operation with assignment:


fun main() {
  var x = 10

  x += 5   // x = x + 5  -> 15
  println("x += 5:  $x")

  x -= 3   // x = x - 3  -> 12
  println("x -= 3:  $x")

  x *= 2   // x = x * 2  -> 24
  println("x *= 2:  $x")

  x /= 4   // x = x / 4  -> 6
  println("x /= 4:  $x")

  x %= 5   // x = x % 5  -> 1
  println("x %= 5:  $x")
}
    

The pattern is always the same: operator followed by =. It's shorthand but also makes your intent clearer — "I'm incrementing x by 5" rather than "I'm assigning x + 5 to x."

Try it Yourself →