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:
if expressionif-elseelse if (elif) statementwhen expression as an alternative to if-elseLet's dive right in! 🏊♂️
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:
if (condition) {
// Code to be executed if condition is true
}For example, let's check if a number is greater than 10:
val number = 15
if (number > 10) {
println("The number is greater than 10.")
}Output:
The number is greater than 10.
The else keyword allows us to execute different code blocks based on the condition's truth value. Here's the syntax:
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:
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.
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:
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:
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 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:
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:
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.
Which Kotlin expression should you use when you want to execute different code blocks based on the condition's truth value?
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! 💡