100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Kotlin

when Expression in Kotlin

Explore Kotlin's when expression, a powerful and flexible replacement for Java's switch statement.

Control FlowBeginner7 min readJul 8, 2026
Analogies

Introduction

The when expression in Kotlin replaces Java's switch statement, but it is far more powerful. Besides matching exact values, when can match ranges, check types with is, evaluate arbitrary boolean conditions, and be used as an expression that returns a value - similar to how if-else can return a value. This makes when one of the most versatile control-flow tools in the language.

🏏

Cricket analogy: A commentator's decision tree does more than match an exact score like a switch: it checks ranges ('50s in this over'), types of dismissal with is CaughtOut, and arbitrary conditions like required run rate, then returns a verdict as a value, much like Kotlin's when expression.

Syntax

kotlin
// As a statement
when (x) {
    1 -> println("One")
    2, 3 -> println("Two or three")
    in 4..10 -> println("Between 4 and 10")
    is String -> println("It's a string")
    else -> println("Something else")
}

// As an expression
val description = when (x) {
    1 -> "One"
    else -> "Not one"
}

Explanation

Each branch of a when block starts with a condition followed by -> and the code (or value) to run when that condition matches. Branches can be comma-separated values, ranges (using in), type checks (using is, which also smart-casts the variable inside that branch), or even arbitrary boolean expressions when no subject is given after when. When when is used as an expression to produce a value, the compiler requires an else branch unless the subject type is exhaustive - for example a sealed class hierarchy or an enum class where every possible case is covered by the other branches.

🏏

Cricket analogy: Each when branch is like an umpire's ruling: comma-separated dismissals, an 'in' range for overs 16..20 (death overs), or 'is RunOut' which smart-casts the dismissal details; when used to return a verdict, an exhaustive sealed DismissalType needs no else branch.

Example

kotlin
fun describe(obj: Any): String = when (obj) {
    1 -> "One"
    "hello" -> "Greeting"
    in 2..10 -> "A number between 2 and 10"
    is Long -> "Long number"
    is String -> "A string: $obj"
    else -> "Unknown"
}

fun main() {
    println(describe(1))
    println(describe("hello"))
    println(describe(5))
    println(describe(42L))
    println(describe("other text"))
}

// Output:
// One
// Greeting
// A number between 2 and 10
// Long number
// A string: other text

Key Takeaways

  • when replaces Java's switch statement and is far more flexible.
  • Branches can match exact values, comma-separated values, ranges, or types (is).
  • when can be used without a subject, acting like a chain of if-else conditions.
  • when used as an expression must be exhaustive - typically requiring an else branch.
  • Type-check branches (is String) automatically smart-cast the variable inside that branch.

Practice what you learned

Was this page helpful?

Topics covered

#Kotlin#KotlinProgrammingStudyNotes#Programming#WhenExpressionInKotlin#Expression#Syntax#Explanation#Example#StudyNotes#SkillVeris