Start learning free

LESSON 27 OF 348 · CALCULATIONS WITH NUMBERS

Increment and Decrement Operators in Kotlin: ++ and --

Lesson Read the full explanation

In programming, increasing the value of a variable by 1 is so common, there is an operator designed to do that.

That operator is the increment operator (++):

var number = 10
number++ // short for number += 1

println(number) // Output: 11

There is also one such operator for decreasing the value of a variable by 1.

That operator is the decrement operator (--):

var number = 10
number-- // short for number -= 1

println(number) // Output: 9

Postfix:

In the examples above, we have used these two operators in their postfix form, with operator (++) after operand (number) such as number++.

If used postfix, the increment operator increments and returns the value before incrementing:

var number = 10

val oldNumber = number++

println(oldNumber) // Output: 10
println(number) // Output: 11

This code first assigns the value of number (which is 10) to oldNumber and then increments number (making it 11). It's equivalent to this:

val oldNumber = number
number++

Prefix:

However, there is also the prefix form, with operator before operand (for example, ++number).

When used prefix, the increment operator increments and returns the value after incrementing:

var number = 10

val newNumber = ++number

println(newNumber) // Output: 11
println(number) // Output: 11

This code first increments number (making it 11) and then assigns the new value of number to oldNumber (making it 11). It's equivalent to this:

number++
val newNumber = number

Here's an example with the decrement operator:

var number = 10

val newNumber = --number

println(newNumber) // Output: 9
println(number) // Output: 9

Assignment
Follow the Coding Tutorial and let's practice with these operators!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 5
    Ctrl + Enter to check

    KOTLIN · CALCULATIONS WITH NUMBERS

    Increment And Decrement Operators in Kotlin, explained

    What they do

    Adding 1 to a variable, or taking 1 away, happens so often that Kotlin has an operator for each. ++ is the increment operator and -- is the decrement operator:

    var number = 10
    number++ // same as number += 1
    println(number)
    
    number-- // same as number -= 1
    println(number)
    
    11
    10
    

    The variable has to be a var, because the operators reassign it. On a val, the compiler reports 'val' cannot be reassigned. and the program doesn't run.

    The most common use is a counter, a variable that goes up by one each time something happens. After var visits = 0 followed by two visits++ statements, visits is 2. The same pattern with -- counts down, for example the number of items left to process. Because the operators say exactly what they do, they are easier to read than visits = visits + 1.

    Postfix and prefix forms

    Both operators can be written after the variable (postfix, number++) or before it (prefix, ++number). On a line of their own, the two forms do the same thing. The difference shows only when you use the value of the expression, for example by storing it in another variable.

    • Postfix increments the variable, but the expression's value is the value before the change.
    • Prefix increments the variable, and the expression's value is the value after the change.
    var number = 10
    val oldNumber = number++
    println(oldNumber)
    println(number)
    
    10
    11
    

    Postfix is the same as taking a copy of the old value, then incrementing:

    val oldNumber = number
    number++
    

    The prefix form increments first and hands out the new value:

    var number = 10
    val newNumber = ++number
    println(newNumber)
    println(number)
    
    11
    11
    

    Decrement behaves the same way. val newNumber = --number with number at 10 leaves both variables at 9, and val old = number-- would give old the value 10 and leave number at 9.

    The lesson's exercise, step by step

    1. Create number with var, set to 18. A val would not work, since every later step changes it.
    2. Apply number++ on its own line. The value becomes 19. Nothing uses the expression's value here, so the postfix and prefix forms are identical.
    3. Store a postfix increment in post. val post = number++ gives post the old value, 19, and then number becomes 20.
    4. Store a prefix increment in pre. val pre = ++number increments first, so number becomes 21 and pre is 21 as well.
    5. Apply the prefix -- to number. On its own line it just lowers number by one, to 20.
    var number = 18
    number++
    val post = number++
    val pre = ++number
    --number
    

    After these lines, post is 19, pre is 21 and number is 20.

    Gotchas

    Mixing both forms in one expression

    Each use of ++ changes the variable at that moment, so an expression with several of them depends on the order of evaluation. With i at 5, i++ + ++i gives 12: the postfix part contributes 5 and makes i 6, then the prefix part makes it 7 and contributes 7. Afterwards i is 7. Code like this is hard to read, so keep each increment in a statement of its own.

    Assigning a postfix result back to the same variable

    With n at 7, the statement n = n++ leaves n at 7. The right side evaluates to the old value and increments the variable, and then the assignment stores the old value back over the new one. The increment is lost. If you want to increment, write n++ alone.

    Only on variables

    The operators change a variable, so the thing they apply to must be one. Writing 5++ makes no sense, because 5 is a value and not a place that can store anything.

    Int overflow

    For an Int, which has 32 bits, incrementing Int.MAX_VALUE wraps around silently to -2147483648. Use a Long if a counter can grow that large.

    Key takeaways

    • ++ adds 1 and -- subtracts 1. They need a var.
    • Alone on a line, prefix and postfix are the same.
    • Used as a value, postfix gives the value before the change and prefix gives the value after it.
    • Keep increments in their own statements, so the code reads in one pass.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free