🔀 Kotlin – If…Else Expressions: Write Conditional Logic Clearly
🧲 Introduction – Why Learn Kotlin If…Else Expressions?
Conditional logic is key to any application—whether it’s checking user input, validating forms, or handling permissions. Kotlin elevates the traditional if...else
by allowing it to be used not just as a statement, but also as an expression. This enables concise and readable code that assigns or returns values directly based on conditions.
🎯 In this guide, you’ll learn:
- Syntax and structure of
if
,if-else
, andif-else-if
- How
if
works as both statement and expression - Best practices for nested and complex conditions
- Real-world usage examples
✅ Basic If Statement in Kotlin
val age = 20
if (age >= 18) {
println("You are an adult.")
}
🟢 Output:
You are an adult.
- Executes the block only when the condition is
true
.
🔁 If…Else Statement
val score = 65
if (score >= 70) {
println("Passed")
} else {
println("Failed")
}
🟢 Output:
Failed
- Executes one of two blocks depending on the condition.
🔀 If…Else…If Ladder
Used when multiple conditions need to be checked.
val marks = 85
if (marks >= 90) {
println("Grade A")
} else if (marks >= 75) {
println("Grade B")
} else if (marks >= 60) {
println("Grade C")
} else {
println("Fail")
}
🟢 Output:
Grade B
✨ Kotlin If as an Expression
In Kotlin, if
can return a value—this makes it an expression rather than just a control statement.
✅ Assigning to a variable:
val num = 10
val result = if (num % 2 == 0) "Even" else "Odd"
println(result)
🟢 Output:
Even
🧠 Expression with Blocks
You can use multi-line if
blocks with return values:
val age = 21
val category = if (age < 13) {
"Child"
} else if (age in 13..19) {
"Teen"
} else {
"Adult"
}
println(category)
🟢 Output:
Adult
✔️ The last expression in each block is returned.
🧪 Nested If Statements
You can nest if
conditions inside each other, but use this with caution.
val isMember = true
val hasPaid = true
if (isMember) {
if (hasPaid) {
println("Access Granted")
} else {
println("Please Pay First")
}
} else {
println("Membership Required")
}
🟢 Output:
Access Granted
✅ Best Practices for if...else
in Kotlin
Practice | Why It Helps |
---|---|
Prefer if as an expression | Makes code more concise and readable |
Avoid deep nesting | Use when for cleaner multi-branch logic |
Use braces {} even for one-liners | Prevents bugs in future edits |
Combine conditions using && and ` |
🚫 Common Mistakes with Kotlin If…Else
❌ Mistake | ✅ Correction |
---|---|
Omitting else when it’s required | Always handle all expected conditions |
Forgetting return value in blocks | Ensure the last line returns a value |
Nesting too deeply | Use when for clarity |
Using assignment = instead of equality == | Always use == in conditions |
📌 Summary – Recap & Next Steps
The if...else
expression in Kotlin is both powerful and expressive. It allows clean inline logic, supports value assignment, and enables better decision-making in your apps.
🔍 Key Takeaways:
- Kotlin treats
if
as an expression that returns a value. if
,else if
, andelse
blocks evaluate in order.- Use
if
for two or three conditions; switch towhen
for more. - Avoid nesting; prefer clean and readable expressions.
⚙️ Practical Use:if...else
expressions are essential for form validation, user permissions, business rule enforcement, and dynamic UI updates in Kotlin apps.
❓ FAQs – Kotlin If…Else
❓ Is Kotlin’s if
a statement or an expression?
✅ Kotlin allows if
to be both—a traditional statement and an expression that returns a value.
❓ Can I assign the result of an if
to a variable?
✅ Yes. Kotlin supports value assignment using if
:
val type = if (age > 18) "Adult" else "Minor"
❓ What if I forget the else
block in an if
expression?
✅ You’ll get a compiler error if the if
is used as an expression. You must cover all cases.
❓ Should I use if...else
or when
?
✅ Use if...else
for 2–3 branches. Use when
when you have multiple cases or conditions.
❓ Does Kotlin require parentheses around conditions?
✅ No. Parentheses are optional:
if (x > 5) { ... } // ✅
Share Now :