🐦 Kotlin
Beginner
What is the when expression in Kotlin?
Answer
The when expression is Kotlin's powerful replacement for Java's switch statement. It is far more expressive — each branch can match a specific value, a range (in 1..10), a type (is String), or an arbitrary condition. When used as an expression (its result is assigned or returned), all cases must be covered and an else branch is required unless all possible values are covered (e.g., with a sealed class or enum). Unlike switch, when branches do not fall through, so you never need break statements.
Previous
What is the difference between == and === in Kotlin?
Next
What is a sealed class in Kotlin?