Kotlin Assignment Operators
Kotlin Assignment Operators are used to assign a value or a computed value to a variable.
In this tutorial, we shall learn about Assignment Operators, their symbols, and how to use them in a Kotlin program.
Table for Assignment Operators
The following table covers Assignment Operators in Kotlin.
Operator | Symbol | Example | Description |
---|---|---|---|
Assignment | = |
x=5 |
Assign a value of 5 to x . |
Addition Assignment | += |
x+=5 |
Add a value of 5 to that of in x . |
Subtraction Assignment | -= |
|
Subtract a value of 5 from that of in x . |
Multiplication Assignment | *= |
|
Multiply a value of 5 to that of in x . |
Division Assignment | /= |
x/=5 |
Divide the value in x with a value of 5 . |
Modulus Assignment | %= |
x%=5 |
Find remainder in the division of x/5 and assign the remainder to x . |
Example Kotlin Program for Assignment Operators
In the following program, we take a variable x
, and apply different assignment operators on it with a value of 5
.
Kotlin Program
fun main() {
var x: Int
// Assignment
x = 21
println("x = $x Assignment")
x = 21
// Addition Assignment
x += 5
println("x = $x Addition Assignment")
x = 21
// Subtraction Assignment
x -= 5
println("x = $x Subtraction Assignment")
x = 21
// Multiplication Assignment
x *= 5
println("x = $x Multiplication Assignment")
x = 21
// Division Assignment
x /= 5
println("x = $x Division Assignment")
x = 21
// Modulus Assignment
x %= 5
println("x = $x Modulus Assignment")
}
Output
x = 21 Assignment
x = 26 Addition Assignment
x = 16 Subtraction Assignment
x = 105 Multiplication Assignment
x = 4 Division Assignment
x = 1 Modulus Assignment
Assignment Operators Tutorials
The following tutorials cover each of the Assignment Operators in detail.