When Expression in Kotlin

Level 296 keystrokes

Uses a when expression, Kotlin's more powerful replacement for switch, to pick a value.

Kotlin code

// The day number to look up
1val day = 3
// when compares day with each branch and returns the matching value
2val name = when (day) {
// value -> result: used when day equals that value
3 1 -> "Monday"
4 2 -> "Tuesday"
// else is required here; day is 3, so name becomes "Other"
5 else -> "Other"
6}
// Prints Other
7println(name)

Output

Other
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Kotlin code and how often they appear.

  • "× 6
  • -× 3
  • >× 3
  • (× 2
  • )× 2
  • =× 2
  • {× 1
  • }× 1

More Kotlin examples at level 2

← Kotlin typing practice