Kotlin if Expression

In this article you will learn to use kotlin if expression with real world example usages.

The syntax of if…else is given by

if (testExpression) {
   // codes to run if testExpression is true
} else {
  // codes to run if testExpression is false
}

The if statement executes a certain section of code if the testExpression is evaluated to true. It can also have an optional else clause, where codes inside the else block are executed if the testExpression is false.

Example: Traditional Usage of if…else

fun main(args: Array<String>) {

    val number = -10

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

Output:

Negative number

Kotlin if expression

Unlike some other languages, Kotlin allows using if as an expression that returns a value:

val result = if (condition) {
    // value to return if condition is true
} else {
    // value to return if condition is false
}

Example: Kotlin if expression

fun main(args: Array<String>) {

    val number = -10

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

    println(result)
}

Output:

Negative number

The else branch is mandatory when using if as an expression. However, if the body of if has only one statement, the curly braces are optional.

Example: Single Statement in if-else

fun main(args: Array<String>) {
    val number = -10
    val result = if (number > 0) "Positive number" else "Negative number"
    println(result)
}

This approach is similar to the ternary operator in Java.

Example: if block With Multiple Expressions

If the block of the if branch contains more than one expression, the last expression is returned as the value of the block:

fun main(args: Array<String>) {

    val a = -9
    val b = -11

    val max = if (a > b) {
        println("$a is larger than $b.")
        println("max variable holds value of a.")
        a
    } else {
        println("$b is larger than $a.")
        println("max variable holds value of b.")
        b
    }
    println("max = $max")
}

Output:

-9 is larger than -11.
max variable holds value of a.
max = -9

Kotlin if..else..if Ladder

You can use if..else…if ladder to return a block of code among many blocks based on conditions:

val result = if (condition1)
    statement1
else if (condition2)
    statement2
else
    statement3

Example: if…else…if Ladder

fun main(args: Array<String>) {

    val number = 0

    val result = if (number > 0)
        "positive number"
    else if (number < 0)
        "negative number"
    else 
        "zero"
    
    println("number is $result")
}

Kotlin Nested if Expression

An if expression can be inside the block of another if expression known as nested if expression:

Example: Nested if Expression

fun main(args: Array<String>) {

    val n1 = 3
    val n2 = 5
    val n3 = -2

    val max = if (n1 > n2) {
        if (n1 > n3)
            n1
        else
            n3
    } else {
        if (n2 > n3)
            n2
        else
            n3
    }

    println("max = $max")
}

Output:

max = 5

These examples demonstrate the versatility and power of if-else statements in Kotlin, allowing you to control program flow and make decisions based on conditions efficiently.