Kotlin if Expression Tutorial 🎯

beginner
20 min

Kotlin if Expression Tutorial 🎯

Welcome to the Kotlin if Expression tutorial! In this lesson, we'll learn how to make decisions in our code using the if expression. This is a fundamental concept in programming and will help you control the flow of your applications.

By the end of this tutorial, you'll be able to:

  • Understand the basic structure of the if expression
  • Learn how to create multiple condition checks using if-else
  • Explore the use of the else if (elif) statement
  • Discover the when expression as an alternative to if-else
  • Practice with real-world examples and quizzes

Let's dive right in! 🏊‍♂️

Basic if Expression 📝

The simplest form of the if expression consists of a condition and code blocks that execute if the condition is true. Here's the syntax:

kotlin
if (condition) { // Code to be executed if condition is true }

For example, let's check if a number is greater than 10:

kotlin
val number = 15 if (number > 10) { println("The number is greater than 10.") }

Output:

The number is greater than 10.

if-else Statement 📝

The else keyword allows us to execute different code blocks based on the condition's truth value. Here's the syntax:

kotlin
if (condition) { // Code to be executed if condition is true } else { // Code to be executed if condition is false }

Let's modify our previous example to display a message if the number is less than or equal to 10:

kotlin
val number = 10 if (number > 10) { println("The number is greater than 10.") } else { println("The number is less than or equal to 10.") }

Output:

The number is less than or equal to 10.

Multiple Conditions with if-else if 📝

You can use the else if (elif) statement to check multiple conditions. The first true condition will determine the executed code block. Here's the syntax:

kotlin
if (condition1) { // Code to be executed if condition1 is true } else if (condition2) { // Code to be executed if condition1 is false and condition2 is true } else if (condition3) { // Code to be executed if condition1 and condition2 are false and condition3 is true } else { // Code to be executed if all conditions are false }

Let's see an example where we check the age of a person:

kotlin
val age = 18 if (age >= 65) { println("You are eligible for senior citizens discount.") } else if (age >= 18 && age <= 64) { println("You are an adult.") } else { println("You are a minor.") }

Output:

You are an adult.

The when Expression 📝

The when expression is another way to perform conditional logic in Kotlin. It is more concise and can be easier to read for simple cases. Here's the syntax:

kotlin
when (expression) { condition1 -> { // Code to be executed if condition1 is true } condition2 -> { // Code to be executed if condition2 is true } // ... else -> { // Code to be executed if no conditions are true } }

Let's rewrite our age example using the when expression:

kotlin
val age = 18 when (age) { in 0..17 -> println("You are a minor.") in 18..64 -> println("You are an adult.") in 65..Int.MAX_VALUE -> println("You are eligible for senior citizens discount.") }

Output:

You are an adult.
Quick Quiz
Question 1 of 1

Which Kotlin expression should you use when you want to execute different code blocks based on the condition's truth value?

Conclusion 🎯

Now you have a solid understanding of the if expression in Kotlin, including the if-else statement, else if, and the when expression. Remember to choose the most appropriate expression based on the complexity of your conditions.

In the next lesson, we'll dive into loops and iterations in Kotlin. Until then, happy coding! 🚀


Remember to check CodeYourCraft for more tutorials, examples, and quizzes to help you master Kotlin programming. Keep practicing, and soon you'll be writing your own impressive applications! 💡