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
// 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
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: CKey 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
1. Why does Kotlin not have a ternary operator like Java's `? :`?
2. What is required when if-else is used as an expression (to produce a value)?
3. What value does a branch of an if-else expression evaluate to?
4. Which of the following is valid Kotlin code?
5. In the example, if `score` is 82, what would `grade` be?
Was this page helpful?
You May Also Like
when Expression in Kotlin
Explore Kotlin's when expression, a powerful and flexible replacement for Java's switch statement.
Operators in Kotlin
Explore Kotlin's arithmetic, comparison, logical, range, and containment operators, including the == vs === distinction.
Null Safety in Kotlin
Kotlin's type system distinguishes nullable from non-nullable types at compile time, eliminating most NullPointerExceptions before code ever runs.
Smart Casts in Kotlin
After a null-check or type-check, Kotlin's compiler automatically treats a variable as the narrowed type within that scope, no explicit cast needed.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics