Kotlin if-else Expression (with Examples)

Kotlin course · Control flow · Lesson 13 of 51

Programs need to make decisions: show an error if a password is too short, give a discount if the cart is big enough, and so on. In Kotlin, you make decisions with if.

Basic if

fun main() {
    val temperature = 32
    if (temperature > 30) {
        println("It's hot today")
    }
    println("Done")
}

Output:

It's hot today
Done

The code inside { } runs only when the condition is true. The condition must be a Boolean: Kotlin doesn’t treat numbers like 0 or 1 as true or false.

if-else

fun main() {
    val age = 15
    if (age >= 18) {
        println("You can vote")
    } else {
        println("You can vote in ${18 - age} years")
    }
}

Output:

You can vote in 3 years

else if: several conditions

Conditions are checked from top to bottom, and only the first matching block runs:

fun main() {
    val marks = 78
    if (marks >= 90) {
        println("Grade A")
    } else if (marks >= 75) {
        println("Grade B")
    } else if (marks >= 50) {
        println("Grade C")
    } else {
        println("Fail")
    }
}

Output:

Grade B

78 also satisfies marks >= 50, but that block never runs because marks >= 75 matched first. Always put the strictest condition first.

if as an expression (Kotlin’s ternary operator)

In Kotlin, if returns a value. That’s why Kotlin has no ? : ternary operator: you don’t need one.

fun main() {
    val a = 10
    val b = 25
    val max = if (a > b) a else b
    println("Max: $max")

    val stock = 0
    val label = if (stock > 0) "In stock" else "Sold out"
    println(label)
}

Output:

Max: 25
Sold out

With blocks, the last expression in each block is the value:

fun main() {
    val cartTotal = 1800
    val shipping = if (cartTotal >= 999) {
        println("Free shipping unlocked")
        0
    } else {
        49
    }
    println("Shipping: ₹$shipping")
}

Output:

Free shipping unlocked
Shipping: ₹0

When if is used as an expression, the else branch is required, because the value must exist in every case.

Nested if

fun main() {
    val isLoggedIn = true
    val isAdmin = false

    if (isLoggedIn) {
        if (isAdmin) {
            println("Welcome to the admin panel")
        } else {
            println("Welcome back")
        }
    } else {
        println("Please log in")
    }
}

Output:

Welcome back

Deep nesting gets hard to read. Often you can combine conditions with &&, or use when, which is covered in the next lesson.

Braces are optional for one line

if (x > 0) println("positive") else println("not positive")

For anything longer than one statement, use braces. They prevent bugs when you add a line later.

Common mistakes

  • Missing else in an expression: val x = if (a > b) a doesn’t compile.
  • Wrong order of else-if: check >= 90 before >= 50, or the first branch catches everything.
  • Using a number as a condition: if (count) doesn’t compile. Write if (count > 0).

Exercise

Write a program that classifies a number as positive, negative or zero, and as even or odd.

fun main() {
    val n = -7
    val sign = if (n > 0) "positive" else if (n < 0) "negative" else "zero"
    val parity = if (n % 2 == 0) "even" else "odd"
    println("$n is $sign and $parity")
}

Output:

-7 is negative and odd

FAQ

Does Kotlin have a ternary operator?

No. Use if as an expression: val result = if (condition) a else b.

Is else required in Kotlin if?

Only when if is used as an expression that returns a value. A plain if statement doesn’t need it.

When should I use when instead of if?

When you check one value against many cases, or have more than two or three else if branches. when is usually cleaner.

WhatsApp us📞 Call now