Branches can return values
Use if or when as an expression when every path should produce a result. This keeps assignment next to the decision and avoids a mutable variable initialized with a meaningless default.
fun pace(minutes: Int): String {
require(minutes >= 0)
return when {
minutes == 0 -> "Rest"
minutes < 25 -> "Review"
minutes < 60 -> "Practice"
else -> "Build"
}
}
Conditions are evaluated in order; the first matching branch wins. Moving minutes < 60 above minutes < 25 would make the smaller-budget branch unreachable. An expression must cover all possibilities, usually with else unless the compiler can prove exhaustiveness for an enum or sealed hierarchy.
Use a subject, such as when (status), when comparing one value against alternatives. Use subjectless when for predicates involving ranges or several inputs.
Exercise
Add a separate if expression to choose singular or plural wording for a lesson count. Test the pace function at 0, 24, 25, 59, and 60.
Check: expected labels are Rest, Review, Practice, Practice, and Build. Explain why branch ordering is part of the function's behavior.