Kotlin's Control Flow Expressions: `if` and `when`

In Kotlin, if and when are expressions that return a value. This lets you assign their result directly to a variable, replacing Java's ternary operator. The footgun: when used as an expression, you must have an else branch to cover all cases.
Why it exists
To reduce boilerplate and mutable state. Traditional control flow statements force you to declare a variable, then mutate it inside different branches. By making control flow an expression that returns a value, Kotlin allows for more concise, functional-style code where values are assigned directly.
The mental model
Think of if and when not just as forks in the road for your code's execution, but as functions that evaluate a condition and return a result. Instead of telling your program "if this, do that; else, do this other thing," you're asking, "based on this condition, what is the resulting value?" This is the same idea as the ternary operator (condition ? value_if_true : value_if_false) in other languages, but more powerful.
How it works
When you use if or when on the right side of an assignment (val result = if (...)), it becomes an expression. The last line of the chosen branch's code block becomes the return value of the entire expression. Because a variable must always be initialized with a value, the compiler enforces that all possible paths are covered. This means an if expression must have an else branch. A when expression must either have an else branch or cover every possible case, which is required when checking enums or sealed classes.
When to use it
Use expressions when you need to determine a single value from multiple possible conditions. This is ideal for initializing val properties, setting function arguments, or returning a value from a function without intermediate mutable variables. For example, assigning a label based on a status: val label = when (status) { "Success" -> "Done"; "Failed" -> "Error"; else -> "In Progress" }.
When not to use it
When your primary goal is to perform side effects, not compute a value, a statement is more appropriate. If your branches only contain print() calls, database writes, or UI updates and don't produce a final value to be used elsewhere, using if/when as a statement is clearer. Trying to force a side-effect-only block into an expression can make code confusing.
One canonical example
A common use case is determining a permission level based on a user role. Instead of a mutable variable, you can assign the permission level directly. val userRole = "Editor"
val permission = when (userRole) {"Viewer" -> "READ_ONLY"
"Editor" -> "READ_WRITE"
else -> "NONE"
}
Here, the when block evaluates the userRole and the entire expression returns a string which is immediately assigned to the immutable variable permission.
Interview question
What is a mandatory requirement when using an `if` expression to assign a value to a variable in Kotlin?
- a.The variable must be declared as `var` to allow reassignment.
- b.An `else` branch must always be present to cover all possible outcomes.Correct
- c.Every branch within the `if` expression must explicitly use the `return` keyword.
- d.Each branch of the `if` expression can only contain a single statement.
Why? this is the answer
When `if` is used as an expression, it must always return a value. To guarantee this, the compiler requires an `else` branch to cover all possible cases. The last line of a branch implicitly becomes its return value, without needing an explicit `return` keyword.
Just read this? Test yourself on what you have been reading.
Read the original → kotlinlang.org
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on kotlin — each one lists the topics its interview covers.
See open roles