Kotlin Subtraction Assignment

Kotlin Subtraction Assignment Operator

Kotlin Subtraction Assignment Operator assigns left operand with the subtraction of left operand from right operand.

Syntax

The syntax to use Subtraction Assignment operator is

x -= value

where

  • x is the variable name
  • -= is the symbol used for Subtraction Assignment operator
  • value is the value that we would like to subtract from that of in variable x

The above expression is equivalent to

x = x - value

Examples

Decrement 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 decrement the value in x by 10.

Kotlin Program

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

Output

11

Explanation

x -= 10
x = x - 10
x = 21 - 10
x = 11