1. Overview

In this tutorial, we’ll focus on the various types of if-else expressions in Kotlin and how they can be used.

2. Traditional Approach

Like other programming languages, we can use if and if-else statements as conditional checking operators.

2.1. If Statement

The if statement specifies a block of code and executes it only if a given condition is true. Otherwise, it ignores the block of code.

Let’s take a look at an example:

val number = 15

if (number > 0) {
    return "Positive number"
}
return "Positive number not found"

2.2. If-Else Statement

The if-else statement contains two blocks of code. We use this statement when we need to perform different actions based on the condition.

When the condition is true, the if statement executes the block of code. Otherwise, the else statement executes the block of code if the condition is false.

Let’s take a look at how this works:

val number = -50

if (number > 0) {
    return "Positive number"
} else {
    return "Negative number"
}

3. Kotlin If-Else Expression

An expression is a combination of one or more values, variables, operators, and functions that execute to produce another value.

val number: Int = 25
val result: Int = 50 + number

Here, 50 + number is an expression that returns an integer value. We can assign the value of the expression to a variable result.

A statement is a unit of a programming language that expresses some action to be carried out.

val result = 50 + 25

Here, val result = 50 + 25 is a statement. The statement contains an expression 50 + 25 that returns an integer value 75. So, expressions are part of the statements.

In Kotlin, if can be used as an expression. While using if as an expression, the else branch is mandatory to avoid compiler error.

In addition, we can assign the result of the if-else expression to a variable:

val number = -50

val result = if (number > 0) {
    "Positive number"
} else {
    "Negative number"
}
return result

The curly braces are optional in case we replace the if-else body by only one statement.

This is similar to a ternary operator in Java. Hence, Kotlin does not have any ternary operator:

val number = -50
val result = if (number > 0) "Positive number" else "Negative number"

return result

3.1. If-Else Block With Multiple Expressions

The if-else branches can have blocks with multiple expressions. In such cases, it returns the last expression as the value of the block.

Let’s have a look at an example:

val x = 24
val y = 73

val result = if (x > y) {
    println("$x is greater than $y")
    x
} else {
    println("$x is less than or equal to $y")
    y
}
return result

4. Kotlin If-Else If-Else Ladder Expressions

We can use the if-else..if-else ladder expression for checking multiple conditions. These conditions are executed from top to bottom.

When a condition is true, it executes the corresponding if expression. If none of the conditions is true, it executes the final else expression.

Let’s see how to use the ladder expression:

val number = 60

val result = if (number < 0) {
    "Negative number"
} else if (number in 0..9) {
    "Single digit number"
} else if (number in 10..99) {
    "Double digit number"
} else {
    "Number has more digits"
}
return result

5. Kotlin Nested Expression

When one expression is present within the body of another expression, then it is called nested expression.

If a condition is true, it executes the code inside the corresponding block. Otherwise, it moves to the next matching condition.

For instance, when an if-else expression is present within the body of an if expression, then it’s called nested if-else expression.

Let’s see how to use a nested expression:

val x = 37
val y = 89
val z = 6

val result = if (x > y) {
    if (x > z)
        x
    else
        z
} else {
    if (y > z)
        y
    else
        z
}
return result

6. Conclusion

In this article, we discussed the various ways of using the if-else expression in Kotlin. Further, we also observed how we can use the if-else expression in Kotlin to write concise code.

As always, the code for these examples is available over on GitHub.

To learn more Kotlin features, have a look at one of our Kotlin tutorials.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.