Increment in Kotlin

Find this useful? Support us: Star on GitHub 6
Category: Number | Language: Kotlin

In Kotlin, we can increment a number by using the unary operator "++" or by adding 1 to the number directly. Here are examples for both:

1. Using the "++" operator:

var num = 5
num++
println(num) // Output: 6

2. Adding 1 to the number directly:

var num = 5
num = num + 1
println(num) // Output: 6

We can also use the shorthand form of adding 1 to the number:

var num = 5
num += 1
println(num) // Output: 6

All three examples will produce the same result, which is incrementing the value of the variable "num" by 1.