100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Kotlin

if-else Expressions in Kotlin

Learn how Kotlin's if-else works as both a statement and a value-returning expression, replacing the ternary operator.

Control FlowBeginner6 min readJul 8, 2026
Analogies

Introduction

In Kotlin, if-else is used for conditional branching just like in most languages, but with a key difference: if is an expression, not just a statement. That means an if-else block can produce a value that you can assign to a variable or return from a function. Because of this, Kotlin has no separate ternary operator (the ? : syntax from Java or C) - a single-line if-else expression fills that role instead.

🏏

Cricket analogy: Like a third umpire's review directly producing the final decision (out/not out) shown on the scoreboard, Kotlin's if-else directly produces a value you can assign, so there's no need for a separate ternary shorthand.

Syntax

kotlin
// As a statement
if (condition) {
    // do something
} else {
    // do something else
}

// As an expression
val result = if (condition) valueIfTrue else valueIfFalse

// Multi-branch
if (condition1) {
    // ...
} else if (condition2) {
    // ...
} else {
    // ...
}

Explanation

When if is used as a statement, the branches perform actions and the return value (if any) is ignored. When used as an expression, the last line of each branch becomes the value of that branch, and both branches must be present and produce a compatible type for the value to be usable. Because Kotlin can infer the type of the whole expression from its branches, val max = if (a > b) a else b reads almost exactly like a ternary expression but stays fully readable with braces when the logic grows more complex.

🏏

Cricket analogy: Like comparing two batsmen's strike rates where val higher = if (rateA > rateB) rateA else rateB picks the winner, both branches must yield a number for the comparison to produce a usable result, unlike a used-and-discarded review.

Example

kotlin
fun main() {
    val a = 10
    val b = 20

    // if-else as an expression
    val max = if (a > b) a else b
    println("Max value is: $max")

    // if-else as a statement with multiple branches
    val score = 72
    val grade = if (score >= 90) {
        "A"
    } else if (score >= 75) {
        "B"
    } else if (score >= 60) {
        "C"
    } else {
        "F"
    }
    println("Grade: $grade")
}

// Output:
// Max value is: 20
// Grade: C

Key Takeaways

  • if in Kotlin can be used as a statement or as an expression that returns a value.
  • Because if-else is an expression, Kotlin does not need a separate ternary (? :) operator.
  • When used as an expression, both the if and else branches are required.
  • The value of a branch is the value of its last expression/line.
  • Chains of else if are simply nested if-else expressions and can also return a value.

Practice what you learned

Was this page helpful?

Topics covered

#Kotlin#KotlinProgrammingStudyNotes#Programming#IfElseExpressionsInKotlin#Else#Expressions#Syntax#Explanation#StudyNotes#SkillVeris