Kotlin when with Conditions

beginner
16 min

Kotlin when with Conditions

Welcome to our comprehensive guide on using the when keyword in Kotlin for condition handling! In this tutorial, we'll learn how to make your code more readable and efficient by mastering the when construct. Let's dive in!

What is the when keyword in Kotlin?

The when keyword in Kotlin is a modern and flexible way to handle conditions in your code. It's similar to the switch statement in other programming languages but offers more features and improved readability.

šŸ’” Pro Tip: Use the when keyword when you want to make your code cleaner and more readable!

Basic usage of when

Let's start with a simple example to understand the basics of using the when keyword:

kotlin
fun main() { val dayOfWeek = 5 when (dayOfWeek) { 1 -> println("Monday") 2 -> println("Tuesday") 3 -> println("Wednesday") 4 -> println("Thursday") 5 -> println("Friday") 6 -> println("Saturday") 7 -> println("Sunday") else -> println("Invalid day of the week") } }

In the above example, we have a variable dayOfWeek that stores an integer representing the day of the week. We use the when keyword to check the value of dayOfWeek and print the corresponding day of the week.

Range checks with in and ..

Kotlin's when keyword supports range checks using the in keyword. This makes it easier to write more concise and readable code:

kotlin
fun main() { val age = 18 when (age) { in 0..12 -> println("You are a child") in 13..17 -> println("You are a teenager") in 18..65 -> println("You are an adult") else -> println("You are a senior citizen") } }

In this example, we use the in keyword to check if the value of age falls within a specific range, and print the corresponding message.

Logical or and and operators

You can use the logical or (||) and and (&&) operators in the when statement to create more complex conditions:

kotlin
fun main() { val grade = 85 when (grade) { in 90..100 -> println("You got an A+ grade!") in 80..89 -> println("You got an A grade!") in 70..79 -> println("You got a B grade!") in 60..69 -> println("You got a C grade!") in 50..59 -> println("You got a D grade!") else -> println("You failed! Better luck next time.") } // Check if the grade is between 80 and 89, or greater than 90 when (grade) { in 80..89 -> println("Your grade is between 80 and 89.") in 90..Int.MAX_VALUE -> println("Your grade is greater than 90.") else -> println("Your grade is below 80.") } }

In this example, we use the logical or (||) and and (&&) operators to create more complex conditions in the when statement.

The is keyword for type checks

The is keyword allows you to perform type checks in the when statement. This is useful when you want to check the type of a variable at runtime:

kotlin
fun main() { val number = 42 when (number) { is Int -> println("It's an integer.") is String -> println("It's a string.") else -> println("It's neither an integer nor a string.") } }

In this example, we use the is keyword to check the type of the variable number.

Default cases with else

You can use the else keyword to specify a default case in your when statement:

kotlin
fun main() { val dayOfWeek = -1 when (dayOfWeek) { 1 -> println("Monday") 2 -> println("Tuesday") 3 -> println("Wednesday") 4 -> println("Thursday") 5 -> println("Friday") 6 -> println("Saturday") 7 -> println("Sunday") else -> println("Invalid day of the week") } }

In this example, we have a variable dayOfWeek with an invalid value (-1), so the else case is executed.

Quiz

Quick Quiz
Question 1 of 1

Which operator do we use in Kotlin's `when` statement to check if a value falls within a specific range?

Conclusion

In this tutorial, we've covered the basics of using the when keyword in Kotlin for condition handling. We've learned how to write more readable and efficient code by mastering the when construct. Practice using the when keyword in your own projects to improve your Kotlin skills!

Happy coding! šŸš€