Translate a parcel status
Before you start this intermediate track
Functions and collections. Read lambdas, nullable values, and conditional expressions.
Guided code reading and debugging. Runnable coding challenges are available in JavaScript, Python, and SQL.
Understand it, then try it.
A parcel service uses numeric status codes but shows people short labels. Kotlin when can be an expression whose selected branch becomes a value. Commas let one branch match several specific codes. An else branch handles every remaining code, ensuring this integer-based expression always produces a label.
fun main() { val status = 202 val label = when (status) { 200, 201 -> "Completed" 202 -> "Queued" else -> "Review" } println(label)}Each mission is a complete independent Kotlin/JVM program with its own main function. Run it with Kotlin 1.9 or later in a Kotlin playground or a configured Kotlin/JVM project. No Android APIs are required.
Track the values line by line. Predict the result before running this example in your own editor.