Kotlin When Statement & Usage Examples | Android Introduction #12

ibrahimcanerdogan
5 min readJan 17, 2024

--

In this reading, you will explore the conditional when statement in more depth. You will learn how to:

  • use when as an expression,
  • use when with a value,
  • and use when with a value as an expression.

UDEMY COURSE

When statement

The when statement is an alternative to if-else-if. On every branch of the when statement, you can specify a predicate and a body. The body will be executed only for the first predicate that returns true. So it works just like if-else-if, but it’s preferred because its syntax is better suited for multiple conditions. And thanks to the fact that it’s a single statement, Kotlin can run this code faster than checking multiple if-else-if statements.

In the following code, if the probability is smaller or equal to 40, then only Unlikely will be printed. If the probability is over 40 but smaller or equal to 80, then only Likely will be printed. If probability is over 80 but smaller or equal to 100, then only Yes will be printed. Otherwise, What? will be printed. If you run the code, you should see Likely because the initial probability is 70, but feel free to change this value and run the code again.

fun main() {
println("Is it going to rain?")
val probability = 70
when {
probability < 40 -> {
println("Unlikely")
}
probability <= 80 -> {
println("Likely")
}
probability < 100 -> {
println("Yes")
}
else -> {
println("What?")
}
}
}

Just like in an if statement, braces are needed only for bodies with more than one statement. If there is only one statement in a body, you can leave out the braces.

fun main() {
println("Is it going to rain?")
val probability = 70
when {
probability < 40 -> println("Unlikely")
probability <= 80 -> println("Likely")
probability < 100 -> println("Yes")
else -> println("What?")
}
}

Using when as an expression

The when statement can also be used as an expression in order to produce a value. The only rule is that a when statement must have an else block so that it has a value to return in case no other block is chosen.

In the following code, the text value depends on which branch is chosen by the when expression.

fun main() {
println("Is it going to rain?")
val probability = 70
val text = when {
probability < 40 -> "Unlikely"
probability <= 80 -> "Likely"
probability < 100 -> "Yes"
else -> "What?"
}
println(text)
}

When with a value

There is also another form of the when statement. If you add a value in brackets after the when keyword, then in each branch, Kotlin compares this value to other values. In the following example, when number is 1, the when statement chooses the branch with a value of 1 and prints, Missed hit. When number is 2, 3, 4 or 5, the second branch is chosen, and Hit with value {number value} is printed. When the value is 6, the last branch is chosen, and Critical hit is printed.

fun main() {
val number = 1 // or 2, 3, 4, 5, 6
when (number) {
1 -> {
println("Missed hit")
}
2, 3, 4, 5 -> {
println("Hit with value $number")
}
6 -> {
println("Critical hit")
}
}
}

Such a when statement can be used as an expression as well. In this example, it is used to produce a text value. Notice that you needed to add an else block for the case when the value of the number variable is not one of the values handled in the other when branches. So if the number is 10, then Unsupported value will be printed.

fun main() {
val number = 1 // or 2, 3, 4, 5, 6
val text = when (number) {
1 -> "Missed hit"
2, 3, 4, 5 -> "Hit with value $number"
6 -> "Critical hit"
else -> "Unsupported value"
}
println(text)
}

Using ranges in when statements

The when expression can be simplified, thanks to the feature known as a range check. In some branches, the condition might be checking if a value is inside a collection or a range. In the following example, instead of 2, 3, 4, , 2..5 was used, so instead of providing values to compare to, it checks if the value is in a range of values from 2 to 5. The syntax is as follows: start with the in keyword, and then specify either a collection or a range that might contain the value or not. Both collections and ranges will be covered later in this course.

fun main() {
val number = 1 // or 2, 3, 4, 5, 6
val text = when (number) {
1 -> "Missed hit"
in 2..5 -> "Hit with value $number"
6 -> "Critical hit"
else -> "Unsupported value"
}
println(text)
}

Type check

As was explained earlier in the course, every value has a certain type. For instance, 123 is of type Int, and “ABC” is of type string. When you have a value, you can check if it is of a certain type using the is keyword. Here are a few examples:

fun main() {
var value = "ABC"

println(value is String) // true
println(value is Int) // false
println(value is Boolean) // false
println(value is Any) // true

value = 123

println(value is String) // false
println(value is Int) // true
println(value is Boolean) // false
println(value is Any) // true
}

Type-check can also be used as a branch in a when statement with a value. This is a popular pattern in Kotlin to decide what action should be performed based on a variable type.

fun main() {
val something: Any = "ABC" // or 123, 0.1, true
when (something) {
is String -> println("This is String")
is Int -> println("This is Int")
is Double -> println("This is Double")
is Boolean -> println("This is Boolean")
}
println(something)
}

In this reading, you learned more about the conditional when statement. You also learned how to use when as an expression, how to use when with a value and how to use when with a value as an expression.

İbrahim Can Erdoğan

LINKEDIN

YOUTUBE

UDEMY

--

--

ibrahimcanerdogan
ibrahimcanerdogan

Written by ibrahimcanerdogan

Hi, My name is Ibrahim, I am developing ebebek android app within Ebebek. I publish various articles in the field of programming and self-improvement.

No responses yet