Introduction
The when expression in Kotlin lets you choose a value based on conditions. It returns a result, so you can use it directly where a value is needed.
The when expression in Kotlin lets you choose a value based on conditions. It returns a result, so you can use it directly where a value is needed.
val result = when (variable) { condition1 -> value1 condition2 -> value2 else -> defaultValue }
The when expression returns the value of the matched branch.
You must cover all cases or use else as a fallback.
day is Saturday or Sunday, otherwise "Weekday".val dayType = when (day) { "Saturday", "Sunday" -> "Weekend" else -> "Weekday" }
x is positive, negative, or zero.val numberType = when { x > 0 -> "Positive" x < 0 -> "Negative" else -> "Zero" }
This program assigns a letter grade based on the numeric score using when as an expression.
fun main() { val score = 85 val grade = when (score) { in 90..100 -> "A" in 80..89 -> "B" in 70..79 -> "C" in 60..69 -> "D" else -> "F" } println("Score: $score") println("Grade: $grade") }
You can use multiple conditions separated by commas in one branch.
The when expression is like a smarter switch that returns a value.
when can return a value directly.
Use it to replace multiple if-else statements for clearer code.
Always handle all cases or provide an else branch.
when expression in Kotlin return when used as an expression?when as expressionwhen can be used as an expression that returns a value depending on which case matches.else branch to avoid exceptions, so it won't throw by default.when returns value [OK]when returns the matched case value [OK]when always returns Unitwhen with if returning Booleanelse branchwhen as an expression returning a value in Kotlin?when syntax with argumentwhen (x) with cases separated by semicolons and an else branch, which is correct.else; val result = when x { 1 -> "One"; else -> "Other" } misses parentheses around x.when syntax uses parentheses and semicolons [OK]val x = 5
val result = when {
x < 0 -> "Negative"
x == 0 -> "Zero"
x in 1..10 -> "Small"
else -> "Large"
}
println(result)result is "Small".when as an expression:val x = 3
val result = when (x) {
1 -> "One"
2 -> "Two"
}
println(result)when expressionwhen expression covers only 1 and 2, but x = 3 is not handled and no else branch is provided.whenwhen must be exhaustive, so missing else causes a compilation error.else branch causes compilation error. -> Option Bwhen needs else [OK]else if not all cases covered [OK]else branchwhen always compiles without elsewhen as an expression. Which code correctly returns "A" for scores 90 and above, "B" for 80-89, "C" for 70-79, and "F" otherwise?when with condition expressionswhen without argument and conditions like score >= 90, which is valid and clear for ranges.when(score) which expects values, not conditions; val grade = when {
in 90..100 -> "A"
in 80..89 -> "B"
in 70..79 -> "C"
else -> "F"
} uses in without variable, which is invalid.when without argument for conditions [OK]when without argument for condition checks [OK]when(variable)in keywordPass the test and this pattern moves unknown