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 operatorvalue
is the value that we would like to subtract from that of in variablex
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