Kotlin when Expression 🎯

beginner
11 min

Kotlin when Expression 🎯

Welcome to this comprehensive guide on the Kotlin when expression! In this tutorial, we'll learn how to use this powerful tool to write more readable and concise code. Let's dive right in! 🐟

What is the Kotlin when Expression? 📝

The when expression in Kotlin is a replacement for the traditional switch-case statements found in other programming languages. It allows you to perform multiple conditional checks in a more organized and readable manner.

Why Use the Kotlin when Expression? 💡

  • Easy to read and maintain: The when expression makes your code more readable by organizing conditions in a clear and logical manner.
  • Reduces code duplication: By combining multiple conditional statements, you can avoid writing repetitive code.
  • Expressive and flexible: The when expression supports a range of comparison operations, making it suitable for a wide variety of use cases.

Basic Syntax 📝

The basic syntax of the Kotlin when expression is as follows:

kotlin
when (expression) { condition1 -> statement1 condition2 -> statement2 // ... else -> elseStatement }
  • expression: This is the value you want to test against the conditions.
  • condition1, condition2, etc.: These are the conditions you want to test.
  • statement1, statement2, etc.: These are the actions to be executed if the corresponding conditions are true.
  • else: This is an optional block that will be executed if no other conditions are met.

Examples 💡

Let's look at a simple example that demonstrates how to use the when expression:

kotlin
fun main() { val number = 5 when (number) { 1 -> println("Number is 1") 2 -> println("Number is 2") 3 -> println("Number is 3") 4 -> println("Number is 4") 5 -> println("Number is 5") else -> println("Number is not between 1 and 4") } }

In this example, we're checking the value of the variable number. Depending on the value, we're printing out a corresponding message.

The in Keyword 💡

The in keyword can be used in the when expression to check if a value is within a given range:

kotlin
fun main() { val number = 5 when (number) { in 1..4 -> println("Number is between 1 and 4") else -> println("Number is not between 1 and 4") } }

In this example, we're checking if the value of number is within the range 1 to 4.

The is Keyword 💡

The is keyword can be used in the when expression to check the type of a value:

kotlin
fun main() { val value = 5 when (value) { is Int -> println("Value is an integer") is String -> println("Value is a string") else -> println("Value is neither an integer nor a string") } }

In this example, we're checking the type of the value. Depending on the type, we're printing out a corresponding message.

Quiz Time! 🎯

Quick Quiz
Question 1 of 1

What does the `when` expression in Kotlin replace?

Summary ✅

In this tutorial, we've learned about the Kotlin when expression, its benefits, basic syntax, and examples. We've also seen how to use the in and is keywords in the when expression. With this knowledge, you're well on your way to writing more readable and efficient code in Kotlin!

Keep learning, keep coding, and happy coding with CodeYourCraft! 🎈