Kotlin Addition Assignment

Kotlin Addition Assignment Operator

Kotlin Addition Assignment Operator assigns left operand with the sum of left and right operands.

Syntax

The syntax to use Addition Assignment operator is

x += value

where

  • x is the variable name
  • += is the symbol used for Addition Assignment operator
  • value is the value that we would like to add to that of in variable x

The above expression is equivalent to

x = x + value

Examples

Increment the value in x by 10

In the following program, we take a variable: x with an initial value of 21. Using Addition Assignment operator, we increment the value in x by 10.

Kotlin Program

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

Output

31

Explanation

x += 10
x = x + 10
x = 21 + 10
x = 31