Menu
Coddy logo textTech

Augmented Assignment

Part of the Fundamentals section of Coddy's Kotlin journey. Lesson 21 of 93.

When you need to update a variable's value based on its current value, Kotlin provides augmented assignment operators. These combine an arithmetic operation with assignment into a single, shorter expression.

Instead of writing x = x + 5, you can write x += 5. Both do the same thing, but the augmented version is cleaner and more common in real code:

fun main() {
    var score = 10
    score += 5   // Same as: score = score + 5
    println(score)  // Prints: 15
}

Each arithmetic operator has an augmented assignment version:

OperatorExampleEquivalent To
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3x = x % 3
fun main() {
    var balance = 100
    balance -= 25   // Subtract 25
    balance *= 2    // Double it
    println(balance)  // Prints: 150
}

Remember, these operators modify the variable, so you must use var, not val.

challenge icon

Challenge

Easy

Write a function applyOperations that takes start, addValue, and multiplyValue and returns the final result after applying augmented assignment operations.

Starting with the initial value, first add addValue to it, then multiply the result by multiplyValue.

Logic:

  1. Start with the start value
  2. Use += to add addValue
  3. Use *= to multiply by multiplyValue

Parameters:

  • start (Int): The initial value
  • addValue (Int): The value to add
  • multiplyValue (Int): The value to multiply by

Returns: The final result after both operations (Int)

Try it yourself

fun applyOperations(start: Int, addValue: Int, multiplyValue: Int): Int {
    // Write code here
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals

Practice on your own: Kotlin playground