Kotlin when as Statement Tutorial 🎯

beginner
14 min

Kotlin when as Statement Tutorial 🎯

Welcome to CodeYourCraft's Kotlin when as Statement tutorial! Today, we're diving into one of Kotlin's most powerful and versatile features. By the end of this lesson, you'll have a solid understanding of how to use the when statement to write cleaner, more efficient code. 📝

What is the Kotlin when as Statement?

The when statement in Kotlin is similar to the switch statement in other programming languages. It's used to compare a given expression with a series of values and execute the corresponding branch of code. 💡

Why use the Kotlin when as Statement?

Using the when statement in Kotlin offers several benefits:

  • Readability: The when statement is more concise and easier to read compared to a series of if-else statements.
  • Expressiveness: With the when statement, you can easily handle complex conditions using pattern matching.
  • Flexibility: The when statement can return a value, making it a great choice for functions that need to return different values based on different conditions.

Syntax and Basic Usage

The basic syntax of the when statement in Kotlin is as follows:

kotlin
when (expression) { value1 -> block1 value2 -> block2 // ... else -> blockN }

In this syntax, expression is the value you want to compare, and value1, value2, etc., are the possible values the expression can take. Each blockX represents the code to be executed if the expression matches the corresponding valueX. The else block is optional and is executed if none of the other conditions are met.

Let's try out a simple example:

kotlin
fun greet(name: String) { when (name.length) { 1 -> println("Hello, $name!") 2 -> println("Hi there, $name!") else -> println("Greetings, $name!") } }

In this example, we've created a greet function that takes a name parameter and prints a greeting based on the length of the name. If the name has 1 character, it prints "Hello," followed by the name. If it has 2 characters, it prints "Hi there," followed by the name. For all other lengths, it prints "Greetings."

Pattern Matching

One of the most powerful features of the when statement in Kotlin is pattern matching. This allows you to match complex expressions, such as ranges, types, and more.

Here's an example using pattern matching to check if a number is even or odd:

kotlin
fun isEven(number: Int) { when (number) { is OddNumber -> println("$number is an odd number.") is EvenNumber -> println("$number is an even number.") else -> println("$number is not a valid number.") } } data class OddNumber(val number: Int) data class EvenNumber(val number: Int)

In this example, we've defined two data classes, OddNumber and EvenNumber, to represent odd and even numbers, respectively. In the isEven function, we use pattern matching to check if the input number is an instance of OddNumber or EvenNumber.

Quiz

Quick Quiz
Question 1 of 1

What is the purpose of the `when` statement in Kotlin?

Wrapping Up

That wraps up our Kotlin when as Statement tutorial! By now, you should have a good understanding of how to use this powerful feature to write cleaner, more efficient code.

As always, practice makes perfect. Take some time to experiment with the when statement in your own projects, and don't hesitate to ask questions if you get stuck!

Good luck, and happy coding! 🚀