The Kotlin when expression is one of the most powerful and expressive control flow features in the Kotlin programming language. It is an advanced and flexible alternative to the traditional switch statement found in many other languages. Kotlin's when expression enhances code readability, safety, and conciseness, making it widely used in Android development, backend services, and general Kotlin applications.
This detailed guide explains the Kotlin when expression from basic to advanced levels, with real-world examples, practical use cases, tables, and FAQs.
The when expression in Kotlin is a conditional expression that evaluates a value against multiple conditions and executes the matching branch. Unlike a traditional switch statement, when can be used as both a statement and an expression, meaning it can return a value.
when (value) { condition1 -> action1 condition2 -> action2 else -> defaultAction }
The when block checks each condition sequentially and executes the first matching branch.
val day = 3 when (day) { 1 -> println("Monday") 2 -> println("Tuesday") 3 -> println("Wednesday") 4 -> println("Thursday") 5 -> println("Friday") else -> println("Weekend") }
This example demonstrates how Kotlin when expression replaces a switch-case structure.
One of the most important features is that Kotlin when can return values.
val score = 85 val grade = when (score) { in 90..100 -> "A" in 75..89 -> "B" in 60..74 -> "C" else -> "Fail" } println(grade)
This makes Kotlin when expression extremely useful for assigning values based on conditions.
Kotlin allows using when without a subject. This is useful for complex conditional logic.
val age = 20 when { age < 13 -> println("Child") age in 13..19 -> println("Teenager") age >= 20 -> println("Adult") }
val number = 5 when (number) { 1, 3, 5, 7, 9 -> println("Odd number") 2, 4, 6, 8 -> println("Even number") else -> println("Unknown") }
Ranges are commonly used in real-world applications like grading systems.
val temperature = 35 when (temperature) { in -10..0 -> println("Freezing") in 1..20 -> println("Cold") in 21..30 -> println("Warm") else -> println("Hot") }
Kotlin when expression can check object types using is.
fun describe(obj: Any) { when (obj) { is Int -> println("Integer value") is String -> println("String value") is Double -> println("Double value") else -> println("Unknown type") } }
val role = "ADMIN" val accessLevel = when (role) { "ADMIN" -> "Full Access" "EDITOR" -> "Edit Access" "VIEWER" -> "Read Only" else -> "No Access" }
val paymentStatus = 2 val message = when (paymentStatus) { 0 -> "Payment Pending" 1 -> "Payment Successful" 2 -> "Payment Failed" else -> "Unknown Status" }
| Feature | Kotlin When | Switch Statement |
|---|---|---|
| Return value | Yes | No |
| Type checking | Supported | Not supported |
| Range support | Yes | No |
| Else mandatory | Recommended | Optional |
The Kotlin when expression is a powerful, readable, and flexible way to handle conditional logic. It replaces traditional switch statements while offering advanced features such as value returns, range checks, type checking, and condition-based execution. Mastering when expressions significantly improves Kotlin code quality and maintainability.
Kotlin when is both. It can execute logic like a statement or return a value like an expression.
Else is mandatory when the when expression is used to return a value and all cases are not covered.
Yes, Kotlin when can completely replace complex if-else chains and improve readability.
Yes, when without an argument allows condition-based checks similar to if-else.
Performance is similar, but Kotlin when offers better flexibility and safety.
Copyrights © 2024 letsupdateskills All rights reserved